home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Component.java < prev    next >
Text File  |  1998-09-22  |  102KB  |  3,061 lines

  1. /*
  2.  * @(#)Component.java    1.185 98/09/09
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.io.PrintStream;
  17. import java.io.PrintWriter;
  18. import java.util.Vector;
  19. import java.util.Locale;
  20. import java.awt.peer.ComponentPeer;
  21. import java.awt.image.ImageObserver;
  22. import java.awt.image.ImageProducer;
  23. import java.awt.image.ColorModel;
  24. import java.awt.event.*;
  25. import java.io.Serializable;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29. import sun.awt.im.InputContext;
  30.  
  31.  
  32. /** 
  33.  * A <em>component</em> is an object having a graphical representation 
  34.  * that can be displayed on the screen and that can interact with the 
  35.  * user. Examples of components are the buttons, checkboxes, and scrollbars 
  36.  * of a typical graphical user interface. <p>
  37.  * The <code>Component</code> class is the abstract superclass of 
  38.  * the nonmenu-related Abstract Window Toolkit components. Class 
  39.  * <code>Component</code> can also be extended directly to create a 
  40.  * lightweight component. A lightweight component is a component that is 
  41.  * not associated with a native opaque window.
  42.  *
  43.  * @version     1.185 98/09/09
  44.  * @author     Arthur van Hoff
  45.  * @author     Sami Shaio
  46.  */
  47. public abstract class Component implements ImageObserver, MenuContainer, 
  48.     Serializable
  49. {
  50.     /**
  51.      * The peer of the component. The peer implements the component's
  52.      * behaviour. The peer is set when the Component is added to a 
  53.      * container that also is a peer.
  54.      * @see #addNotify
  55.      * @see #removeNotify
  56.      */
  57.     transient ComponentPeer peer;
  58.  
  59.     /**
  60.      * The parent of the object. It may be null for top-level components.
  61.      * @see #getParent
  62.      */
  63.     transient Container parent;
  64.  
  65.     /**
  66.      * The x position of the component in the parent's coordinate system.
  67.      * @see #getLocation
  68.      */
  69.     int x;
  70.  
  71.     /**
  72.      * The y position of the component in the parent's coordinate system.
  73.      * @see #getLocation
  74.      */
  75.     int y;
  76.  
  77.     /**
  78.      * The width of the component.
  79.      * @see #getSize
  80.      */
  81.     int width;
  82.  
  83.     /**
  84.      * The height of the component.
  85.      * @see #getSize
  86.      */
  87.     int height;
  88.  
  89.     /**
  90.      * The foreground color for this component.
  91.      * @see #getForeground
  92.      * @see #setForeground
  93.      */
  94.     Color    foreground;
  95.  
  96.     /**
  97.      * The background color for this component.
  98.      * @see #getBackground
  99.      * @see #setBackground
  100.      */
  101.     Color    background;
  102.  
  103.     /**
  104.      * The font used by this component.
  105.      * @see #getFont
  106.      * @see #setFont
  107.      */
  108.     Font    font;
  109.  
  110.     /**
  111.      * The cursor displayed when pointer is over this component.
  112.      * @see #getCursor
  113.      * @see #setCursor
  114.      */
  115.     Cursor    cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  116.  
  117.     /**
  118.      * The locale for the component.
  119.      * @see #getLocale
  120.      * @see #setLocale
  121.      */
  122.     Locale      locale;
  123.  
  124.     /**
  125.      * True when the object is visible. An object that is not
  126.      * visible is not drawn on the screen.
  127.      * @see #isVisible
  128.      * @see #setVisible
  129.      */
  130.     boolean visible = true;
  131.  
  132.     /**
  133.      * True when the object is enabled. An object that is not
  134.      * enabled does not interact with the user.
  135.      * @see #isEnabled
  136.      * @see #setEnabled
  137.      */
  138.     boolean enabled = true;
  139.  
  140.     /** 
  141.      * True when the object is valid. An invalid object needs to
  142.      * be layed out. This flag is set to false when the object
  143.      * size is changed.
  144.      * @see #isValid
  145.      * @see #validate
  146.      * @see #invalidate
  147.      */
  148.     boolean valid = false;
  149.  
  150.     Vector popups;
  151.  
  152.     private String name;
  153.     private boolean nameExplicitlySet = false;
  154.  
  155.     /**
  156.      * The locking object for AWT component-tree and layout operations.
  157.      *
  158.      * @see #getTreeLock
  159.      */
  160.     static final Object LOCK = new Object();
  161.  
  162.     /** Internal, cached size information */
  163.     Dimension minSize;
  164.  
  165.     /** Internal, cached size information */
  166.     Dimension prefSize;
  167.  
  168.     boolean newEventsOnly = false;
  169.     transient ComponentListener componentListener;
  170.     transient FocusListener focusListener;
  171.     transient KeyListener keyListener;
  172.     transient MouseListener mouseListener;
  173.     transient MouseMotionListener mouseMotionListener;
  174.  
  175.     /** Internal, constants for serialization */
  176.     final static String actionListenerK = "actionL";
  177.     final static String adjustmentListenerK = "adjustmentL";
  178.     final static String componentListenerK = "componentL";
  179.     final static String containerListenerK = "containerL";
  180.     final static String focusListenerK = "focusL";
  181.     final static String itemListenerK = "itemL";
  182.     final static String keyListenerK = "keyL";
  183.     final static String mouseListenerK = "mouseL";
  184.     final static String mouseMotionListenerK = "mouseMotionL";
  185.     final static String textListenerK = "textL";
  186.     final static String windowListenerK = "windowL";
  187.  
  188.  
  189.     // The eventMask is ONLY set by subclasses via enableEvents.
  190.     // The mask should NOT be set when listeners are registered
  191.     // so that we can distinguish the difference between when
  192.     // listeners request events and subclasses request them.
  193.     long eventMask;
  194.  
  195.     /**
  196.      * Static properties for incremental drawing.
  197.      * @see #imageUpdate
  198.      */
  199.     static boolean isInc;
  200.     static int incRate;
  201.     static {
  202.     String s;
  203.  
  204.     s = System.getProperty("awt.image.incrementaldraw");
  205.     isInc = (s == null || s.equals("true"));
  206.  
  207.     s = System.getProperty("awt.image.redrawrate");
  208.     incRate = (s != null) ? Integer.parseInt(s) : 100;
  209.     }
  210.  
  211.     /**
  212.      * Ease-of-use constant for <code>getAlignmentY()</code>.  Specifies an
  213.      * alignment to the top of the component.
  214.      * @see     #getAlignmentY
  215.      */
  216.     public static final float TOP_ALIGNMENT = 0.0f;
  217.  
  218.     /**
  219.      * Ease-of-use constant for <code>getAlignmentY</code> and 
  220.      * <code>getAlignmentX</code>. Specifies an alignment to 
  221.      * the center of the component
  222.      * @see     #getAlignmentX
  223.      * @see     #getAlignmentY
  224.      */
  225.     public static final float CENTER_ALIGNMENT = 0.5f;
  226.  
  227.     /**
  228.      * Ease-of-use constant for <code>getAlignmentY</code>.  Specifies an
  229.      * alignment to the bottom of the component.
  230.      * @see     #getAlignmentY
  231.      */
  232.     public static final float BOTTOM_ALIGNMENT = 1.0f;
  233.  
  234.     /**
  235.      * Ease-of-use constant for <code>getAlignmentX</code>.  Specifies an
  236.      * alignment to the left side of the component.
  237.      * @see     #getAlignmentX
  238.      */
  239.     public static final float LEFT_ALIGNMENT = 0.0f;
  240.  
  241.     /**
  242.      * Ease-of-use constant for <code>getAlignmentX</code>.  Specifies an
  243.      * alignment to the right side of the component.
  244.      * @see     #getAlignmentX
  245.      */
  246.     public static final float RIGHT_ALIGNMENT = 1.0f;
  247.  
  248.     /*
  249.      * JDK 1.1 serialVersionUID 
  250.      */
  251.     private static final long serialVersionUID = -7644114512714619750L;
  252.  
  253.     /**
  254.      * Constructs a new component. Class <code>Component</code> can be 
  255.      * extended directly to create a lightweight component that does not 
  256.      * utilize an opaque native window. A lightweight component must be 
  257.      * hosted by a native container somewhere higher up in the component 
  258.      * tree (for example, by a <code>Frame</code> object).
  259.      */
  260.     protected Component() {
  261.     }
  262.  
  263.     /**
  264.      * Construct a name for this component.  Called by getName() when the
  265.      * name is null.
  266.      */
  267.     String constructComponentName() {
  268.     return null; // For strict compliance with prior JDKs, a Component
  269.                  // that doesn't set its name should return null from
  270.                  // getName();
  271.     }
  272.  
  273.     /**
  274.      * Gets the name of the component.
  275.      * @return This component's name.
  276.      * @see    #setName
  277.      * @since JDK1.1
  278.      */
  279.     public String getName() {
  280.     if (name == null && !nameExplicitlySet) {
  281.         synchronized(this) {
  282.         if (name == null && !nameExplicitlySet)
  283.             name = constructComponentName();
  284.         }
  285.     }
  286.         return name;
  287.     }
  288.  
  289.     /**
  290.      * Sets the name of the component to the specified string.
  291.      * @param <code>name</code>  The string that is to be this 
  292.      * component's name.
  293.      * @see #getName
  294.      * @since JDK1.1
  295.      */
  296.     public void setName(String name) {
  297.     synchronized(this) {
  298.         this.name = name;
  299.         nameExplicitlySet = true;
  300.     }
  301.     }
  302.  
  303.     /**
  304.      * Gets the parent of this component.
  305.      * @return The parent container of this component.
  306.      * @since JDK1.0
  307.      */
  308.     public Container getParent() {
  309.     return parent;
  310.     }
  311.  
  312.     /**
  313.      * @deprecated As of JDK version 1.1,
  314.      * programs should not directly manipulate peers.
  315.      */
  316.     public ComponentPeer getPeer() {
  317.     return peer;
  318.     }
  319.  
  320.     /**
  321.      * Gets this component's locking object (the object that owns the thread 
  322.      * sychronization monitor) for AWT component-tree and layout
  323.      * operations.
  324.      * @return This component's locking object.
  325.      */
  326.     public final Object getTreeLock() {
  327.     return LOCK;
  328.     }
  329.  
  330.     /**
  331.      * Gets the toolkit of this component. Note that
  332.      * the frame that contains a component controls which
  333.      * toolkit is used by that component. Therefore if the component  
  334.      * is moved from one frame to another, the toolkit it uses may change.
  335.      * @return  The toolkit of this component.
  336.      * @since JDK1.0
  337.      */
  338.     public Toolkit getToolkit() {
  339.           ComponentPeer peer = this.peer;
  340.     if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)){
  341.         return peer.getToolkit();
  342.     }
  343.     Container parent = this.parent;
  344.     if (parent != null) {
  345.         return parent.getToolkit();
  346.     }
  347.     return Toolkit.getDefaultToolkit();
  348.     }
  349.  
  350.     /**
  351.      * Determines whether this component is valid. Components are 
  352.      * invalidated when they are first shown on the screen.
  353.      * @return <code>true</code> if the component is valid; <code>false</code> 
  354.      * otherwise.
  355.      * @see #validate
  356.      * @see #invalidate
  357.      * @since JDK1.0
  358.      */
  359.     public boolean isValid() {
  360.     return (peer != null) && valid;
  361.     }
  362.  
  363.     /**
  364.      * Determines whether this component is visible. Components are 
  365.      * initially visible, with the exception of top level components such 
  366.      * as <code>Frame</code> objects.
  367.      * @return <code>true</code> if the component is visible; 
  368.      * <code>false</code> otherwise.
  369.      * @see #setVisible
  370.      * @since JDK1.0
  371.      */
  372.     public boolean isVisible() {
  373.     return visible;
  374.     }
  375.  
  376.     /**
  377.      * Determines whether this component is showing on screen. This means 
  378.      * that the component must be visible, and it must be in a container 
  379.      * that is visible and showing.
  380.      * @return <code>true</code> if the component is showing; 
  381.      * <code>false</code> otherwise.
  382.      * @see #setVisible
  383.      * @since JDK1.0
  384.      */
  385.     public boolean isShowing() {
  386.     if (visible && (peer != null)) {
  387.             Container parent = this.parent;
  388.         return (parent == null) || parent.isShowing();
  389.     }
  390.     return false;
  391.     }
  392.  
  393.     /**
  394.      * Determines whether this component is enabled. An enabled component 
  395.      * can respond to user input and generate events. Components are 
  396.      * enabled initially by default. A component may be enabled or disabled by 
  397.      * calling its <code>setEnabled</code> method.
  398.      * @return <code>true</code> if the component is enabled; 
  399.      * <code>false</code> otherwise.
  400.      * @see #setEnabled
  401.      * @since JDK1.0
  402.      */
  403.     public boolean isEnabled() {
  404.     return enabled;
  405.     }
  406.  
  407.     /**
  408.      * Enables or disables this component, depending on the value of the 
  409.      * parameter <code>b</code>. An enabled component can respond to user 
  410.      * input and generate events. Components are enabled initially by default.
  411.      * @param     <code>b</code>   If <code>true</code>, this component is 
  412.      *            enabled; otherwise this component is disabled.
  413.      * @see #isEnabled
  414.      * @since JDK1.1
  415.      */
  416.     public void setEnabled(boolean b) {
  417.         enable(b);
  418.     }
  419.  
  420.     /**
  421.      * @deprecated As of JDK version 1.1,
  422.      * replaced by <code>setEnabled(boolean)</code>.
  423.      */
  424.     public void enable() {
  425.         if (enabled != true) {
  426.         //synchronized (getTreeLock()) { // Removed for Bug #4114201
  427.         enabled = true;
  428.         ComponentPeer peer = this.peer;
  429.         if (peer != null) {
  430.             peer.enable();
  431.         }
  432.         //}
  433.     }
  434.     }
  435.  
  436.     /**
  437.      * @deprecated As of JDK version 1.1,
  438.      * replaced by <code>setEnabled(boolean)</code>.
  439.      */
  440.     public void enable(boolean b) {
  441.     if (b) {
  442.         enable();
  443.     } else {
  444.         disable();
  445.     }
  446.     }
  447.  
  448.     /**
  449.      * @deprecated As of JDK version 1.1,
  450.      * replaced by <code>setEnabled(boolean)</code>.
  451.      */
  452.     public void disable() {
  453.         if (enabled != false) {
  454.         //synchronized (getTreeLock()) { //Removed for Bug #4114201
  455.         enabled = false;
  456.         ComponentPeer peer = this.peer;
  457.         if (peer != null) {
  458.             peer.disable();
  459.         }
  460.         //}
  461.     }
  462.     }
  463.  
  464.     /**
  465.      * Shows or hides this component depending on the value of parameter 
  466.      * <code>b</code>.
  467.      * @param <code>b</code>  If <code>true</code>, shows this component; 
  468.      * otherwise, hides this component.
  469.      * @see #isVisible
  470.      * @since JDK1.1
  471.      */
  472.     public void setVisible(boolean b) {
  473.         show(b);
  474.     }
  475.  
  476.     /**
  477.      * @deprecated As of JDK version 1.1,
  478.      * replaced by <code>setVisible(boolean)</code>.
  479.      */
  480.     public void show() {
  481.     if (visible != true) {
  482.         //synchronized (getTreeLock()) { //Removed for Bug #4114201
  483.         visible = true;
  484.                 ComponentPeer peer = this.peer;
  485.         if (peer != null) {
  486.             peer.show();
  487.             if (peer instanceof java.awt.peer.LightweightPeer) {
  488.             repaint();
  489.             }
  490.         }
  491.                 if (componentListener != null ||
  492.                     (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {                   
  493.                     ComponentEvent e = new ComponentEvent(this, 
  494.                                      ComponentEvent.COMPONENT_SHOWN);
  495.                     Toolkit.getEventQueue().postEvent(e);
  496.                 }
  497.         //}
  498.             Container parent = this.parent;
  499.         if (parent != null) {
  500.         parent.invalidate();
  501.         }
  502.     }
  503.     }
  504.  
  505.     /**
  506.      * @deprecated As of JDK version 1.1,
  507.      * replaced by <code>setVisible(boolean)</code>.
  508.      */
  509.     public void show(boolean b) {
  510.     if (b) {
  511.         show();
  512.     } else {
  513.         hide();  
  514.     }
  515.     }
  516.  
  517.     /**
  518.      * @deprecated As of JDK version 1.1,
  519.      * replaced by <code>setVisible(boolean)</code>.
  520.      */
  521.     public void hide() {
  522.     if (visible != false) {
  523.         synchronized (getTreeLock()) {
  524.         visible = false;
  525.                 ComponentPeer peer = this.peer;
  526.         if (peer != null) {
  527.             peer.hide();
  528.             if (peer instanceof java.awt.peer.LightweightPeer) {
  529.             repaint();
  530.             }
  531.         }
  532.                 if (componentListener != null ||
  533.                     (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {             
  534.                     ComponentEvent e = new ComponentEvent(this, 
  535.                                      ComponentEvent.COMPONENT_HIDDEN);
  536.                     Toolkit.getEventQueue().postEvent(e);
  537.                 }
  538.                 Container parent = this.parent;
  539.             if (parent != null) {
  540.             parent.invalidate();
  541.                 }
  542.         }
  543.     }
  544.     }
  545.  
  546.     /**
  547.      * Gets the foreground color of this component.
  548.      * @return This component's foreground color. If this component does 
  549.      * not have a foreground color, the foreground color of its parent 
  550.      * is returned.
  551.      * @see #java.awt.Component#setForeground(java.awt.Color)
  552.      * @since JDK1.0
  553.      */
  554.     public Color getForeground() {
  555.           Color foreground = this.foreground;
  556.     if (foreground != null) {
  557.         return foreground;
  558.     }
  559.         Container parent = this.parent;
  560.     return (parent != null) ? parent.getForeground() : null;
  561.     }
  562.  
  563.     /** 
  564.      * Sets the foreground color of this component.
  565.      * @param <code>c</code> The color to become this component's 
  566.      * foreground color.
  567.      * @see #getForeground
  568.      * @since JDK1.0
  569.      */
  570.     public void setForeground(Color c) {
  571.     ComponentPeer peer = this.peer;
  572.     foreground = c;
  573.     if (peer != null) {
  574.         c = getForeground();
  575.         if (c != null) {
  576.         peer.setForeground(c);
  577.         }
  578.     }
  579.     }
  580.  
  581.     /**
  582.      * Gets the background color of this component.
  583.      * @return This component's background color. If this component does 
  584.      * not have a background color, the background color of its parent 
  585.      * is returned.
  586.      * @see java.awt.Component#setBackground(java.awt.Color)
  587.      * @since JDK1.0
  588.      */
  589.     public Color getBackground() {
  590.         Color background = this.background;
  591.     if (background != null) {
  592.         return background;
  593.     }
  594.         Container parent = this.parent;
  595.     return (parent != null) ? parent.getBackground() : null;
  596.     }
  597.  
  598.     /** 
  599.      * Sets the background color of this component.
  600.      * @param <code>c</code> The color to become this component's 
  601.      * background color.
  602.      * @see #getBackground
  603.      * @since JDK1.0
  604.      */
  605.     public void setBackground(Color c) {
  606.     ComponentPeer peer = this.peer;
  607.     background = c;
  608.     if (peer != null) {
  609.         c = getBackground();
  610.         if (c != null) {
  611.         peer.setBackground(c);
  612.         }
  613.     }
  614.     }
  615.  
  616.     /**
  617.      * Gets the font of this component.
  618.      * @return This component's font. If a font has not been set 
  619.      * for this component, the font of its parent is returned.
  620.      * @see #setFont
  621.      * @since JDK1.0
  622.      */
  623.     public Font getFont() {
  624.         Font font = this.font;
  625.     if (font != null) {
  626.         return font;
  627.     }
  628.         Container parent = this.parent;
  629.     return (parent != null) ? parent.getFont() : null;
  630.     }
  631.  
  632.     /** 
  633.      * Sets the font of this component.
  634.      * @param <code>f</code> The font to become this component's font.
  635.      * @see #getFont
  636.      * @since JDK1.0
  637.      */
  638.     public void setFont(Font f) {
  639.         synchronized (getTreeLock()) {
  640.         ComponentPeer peer = this.peer;
  641.         font = f;
  642.         if (peer != null) {
  643.             f = getFont();
  644.         if (f != null) {
  645.             peer.setFont(f);
  646.         }
  647.         }
  648.     }
  649.     }
  650.  
  651.     /**
  652.      * Gets the locale of this component.
  653.      * @return This component's locale. If this component does not 
  654.      * have a locale, the locale of its parent is returned.
  655.      * @see #setLocale
  656.      * @exception IllegalComponentStateException If the Component 
  657.      * does not have its own locale and has not yet been added to
  658.      * a containment hierarchy such that the locale can be determined
  659.      * from the containing parent.
  660.      * @since  JDK1.1
  661.      */
  662.   public Locale getLocale() {
  663.         Locale locale = this.locale;
  664.     if (locale != null) {
  665.       return locale;
  666.     }
  667.         Container parent = this.parent;
  668.  
  669.     if (parent == null) {
  670.         throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
  671.     } else {
  672.         return parent.getLocale();
  673.     }
  674.     }
  675.  
  676.     /** 
  677.      * Sets the locale of this component.
  678.      * @param <code>l</code> The locale to become this component's locale.
  679.      * @see #getLocale
  680.      * @since JDK1.1
  681.      */
  682.     public void setLocale(Locale l) {
  683.     locale = l;
  684.     }
  685.  
  686.     /**
  687.      * Gets the instance of <code>ColorModel</code> used to display 
  688.      * the component on the output device.
  689.      * @return The color model used by this component.
  690.      * @see java.awt.image.ColorModel
  691.      * @see java.awt.peer.ComponentPeer#getColorModel()
  692.      * @see java.awt.Toolkit#getColorModel()
  693.      * @since JDK1.0
  694.      */
  695.     public ColorModel getColorModel() {
  696.         ComponentPeer peer = this.peer;
  697.     if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  698.         return peer.getColorModel();
  699.     }
  700.     return getToolkit().getColorModel();
  701.     }
  702.  
  703.     /** 
  704.      * Gets the location of this component in the form of a 
  705.      * point specifying the component's top-left corner.
  706.      * The location will be relative to the parent's coordinate space.
  707.      * @return An instance of <code>Point</code> representing 
  708.      * the top-left corner of the component's bounds in the coordinate 
  709.      * space of the component's parent.
  710.      * @see #setLocation
  711.      * @see #getLocationOnScreen
  712.      * @since JDK1.1
  713.      */
  714.     public Point getLocation() {
  715.     return location();
  716.     }
  717.  
  718.     /** 
  719.      * Gets the location of this component in the form of a point 
  720.      * specifying the component's top-left corner in the screen's 
  721.      * coordinate space.
  722.      * @return An instance of <code>Point</code> representing 
  723.      * the top-left corner of the component's bounds in the 
  724.      * coordinate space of the screen.
  725.      * @see #setLocation
  726.      * @see #getLocation
  727.      */
  728.     public Point getLocationOnScreen() {
  729.     synchronized (getTreeLock()) {
  730.         if (peer != null && isShowing()) {
  731.         if (peer instanceof java.awt.peer.LightweightPeer) {
  732.             // lightweight component location needs to be translated 
  733.             // relative to a native component.
  734.             Container host = getNativeContainer();
  735.             Point pt = host.peer.getLocationOnScreen();
  736.             for(Component c = this; c != host; c = c.getParent()) {
  737.             pt.x += c.x;
  738.             pt.y += c.y;
  739.             }
  740.             return pt;
  741.         } else {
  742.             Point pt = peer.getLocationOnScreen();
  743.             return pt;
  744.         }
  745.         } else {
  746.             throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
  747.         }
  748.     }
  749.     }
  750.  
  751.  
  752.     /** 
  753.      * @deprecated As of JDK version 1.1,
  754.      * replaced by <code>getLocation()</code>.
  755.      */
  756.     public Point location() {
  757.     return new Point(x, y);
  758.     }
  759.  
  760.     /** 
  761.      * Moves this component to a new location. The top-left corner of 
  762.      * the new location is specified by the <code>x</code> and <code>y</code> 
  763.      * parameters in the coordinate space of this component's parent.
  764.      * @param <code>x</code> The <i>x</i>-coordinate of the new location's 
  765.      * top-left corner in the parent's coordinate space.
  766.      * @param <code>y</code> The <i>y</i>-coordinate of the new location's 
  767.      * top-left corner in the parent's coordinate space.
  768.      * @see #getLocation
  769.      * @see #setBounds
  770.      * @since JDK1.1
  771.      */
  772.     public void setLocation(int x, int y) {
  773.     move(x, y);
  774.     }
  775.  
  776.     /**
  777.      * @deprecated As of JDK version 1.1, 
  778.      * replaced by <code>setLocation(int, int)</code>.
  779.      */
  780.     public void move(int x, int y) {
  781.     setBounds(x, y, width, height);
  782.     }
  783.  
  784.     /** 
  785.      * Moves this component to a new location. The top-left corner of 
  786.      * the new location is specified by point <code>p</code>. Point 
  787.      * <code>p</code> is given in the parent's coordinate space.
  788.      * @param <code>p</code> The point defining the top-left corner 
  789.      * of the new location, given in the coordinate space of this 
  790.      * component's parent.
  791.      * @see #getLocation
  792.      * @see #setBounds
  793.      * @since JDK1.1
  794.      */
  795.     public void setLocation(Point p) {
  796.         setLocation(p.x, p.y);
  797.     }
  798.  
  799.     /** 
  800.      * Returns the size of this component in the form of a 
  801.      * <code>Dimension</code> object. The <code>height</code> 
  802.      * field of the <code>Dimension</code> object contains 
  803.      * this component's height, and the <code>width</code> 
  804.      * field of the <code>Dimension</code> object contains 
  805.      * this component's width.
  806.      * @return A <code>Dimension</code> object that indicates the 
  807.      * size of this component.
  808.      * @see #setSize
  809.      * @since JDK1.1
  810.      */
  811.     public Dimension getSize() {
  812.     return size();
  813.     }
  814.  
  815.     /** 
  816.      * @deprecated As of JDK version 1.1,
  817.      * replaced by <code>getSize()</code>.
  818.      */
  819.     public Dimension size() {
  820.     return new Dimension(width, height);
  821.     }
  822.  
  823.     /**
  824.      * Resizes this component so that it has width <code>width</code> 
  825.      * and <code>height</code>.
  826.      * @param <code>width</code> The new width of this component in pixels.
  827.      * @param <code>height</code> The new height of this component in pixels.
  828.      * @see #getSize
  829.      * @see #setBounds
  830.      * @since JDK1.1
  831.      */
  832.     public void setSize(int width, int height) {
  833.     resize(width, height);
  834.     }
  835.  
  836.     /**
  837.      * @deprecated As of JDK version 1.1,
  838.      * replaced by <code>setSize(int, int)</code>.
  839.      */
  840.     public void resize(int width, int height) {
  841.     setBounds(x, y, width, height);
  842.     }
  843.  
  844.     /** 
  845.      * Resizes this component so that it has width <code>d.width</code> 
  846.      * and height <code>d.height</code>.
  847.      * @param <code>d</code> The dimension specifying the new size 
  848.      * of this component.
  849.      * @see #setSize
  850.      * @see #setBounds
  851.      * @since JDK1.1
  852.      */
  853.     public void setSize(Dimension d) {
  854.     resize(d);
  855.     }
  856.  
  857.     /** 
  858.      * @deprecated As of JDK version 1.1,
  859.      * replaced by <code>setSize(Dimension)</code>.
  860.      */
  861.     public void resize(Dimension d) {
  862.     setSize(d.width, d.height);
  863.     }
  864.  
  865.     /** 
  866.      * Gets the bounds of this component in the form of a 
  867.      * <code>Rectangle</code> object. The bounds specify this 
  868.      * component's width, height, and location relative to 
  869.      * its parent.
  870.      * @return A rectangle indicating this component's bounds.
  871.      * @see #setBounds
  872.      * @see #getLocation
  873.      * @see #getSize
  874.      */
  875.     public Rectangle getBounds() {
  876.     return bounds();
  877.     }
  878.  
  879.     /**
  880.      * @deprecated As of JDK version 1.1, 
  881.      * replaced by <code>getBounds()</code>.
  882.      */
  883.     public Rectangle bounds() {
  884.     return new Rectangle(x, y, width, height);
  885.     }
  886.  
  887.     /** 
  888.      * Moves and resizes this component. The new location of the top-left 
  889.      * corner is specified by <code>x</code> and <code>y</code>, and the 
  890.      * new size is specified by <code>width</code> and <code>height</code>.
  891.      * @param <code>x</code> The new <i>x</i>-coordinate of this component.
  892.      * @param <code>y</code> The new <i>y</i>-coordinate of this component.
  893.      * @param <code>width</code> The new <code>width</code> of this component.
  894.      * @param <code>height</code> The new <code>height</code> of this 
  895.      * component.
  896.      * @see java.awt.Component#getBounds
  897.      * @see java.awt.Component#setLocation(int, int)
  898.      * @see java.awt.Component#setLocation(java.awt.Point)
  899.      * @see java.awt.Component#setSize(int, int)
  900.      * @see java.awt.Component#setSize(java.awt.Dimension)
  901.      * @JDK1.1
  902.      */
  903.     public void setBounds(int x, int y, int width, int height) {
  904.     reshape(x, y, width, height);
  905.     }
  906.  
  907.     /** 
  908.      * @deprecated As of JDK version 1.1,
  909.      * replaced by <code>setBounds(int, int, int, int)</code>.
  910.      */
  911.     public void reshape(int x, int y, int width, int height) {
  912.     synchronized (getTreeLock()) {
  913.         boolean resized = (this.width != width) || (this.height != height);
  914.             boolean moved = (this.x != x) || (this.y != y);
  915.  
  916.         if (resized || moved) {
  917.                 boolean isLightweight = 
  918.                     (peer instanceof java.awt.peer.LightweightPeer);
  919.  
  920.                 // Remember the area this component occupied in its parent.
  921.                 int oldParentX = this.x;
  922.                 int oldParentY = this.y;
  923.                 if (parent != null) {
  924.                     oldParentX += parent.x;
  925.                     oldParentY += parent.y;
  926.                 }
  927.                 int oldWidth = this.width;
  928.                 int oldHeight = this.height;
  929.         this.x = x;
  930.         this.y = y;
  931.         this.width = width;
  932.         this.height = height;
  933.         if (peer != null) {
  934.             if (isLightweight) {
  935.             peer.setBounds(x, y, width, height);
  936.             } else {
  937.             // native peer might be offset by more than direct
  938.             // parent since parent might be lightweight.
  939.             int nativeX = x;
  940.             int nativeY = y;
  941.             for(Component c = parent; (c != null) &&
  942.                 (c.peer instanceof java.awt.peer.LightweightPeer); 
  943.                 c = c.parent) {
  944.  
  945.                 nativeX += c.x;
  946.                 nativeY += c.y;
  947.             }
  948.             peer.setBounds(nativeX, nativeY, width, height);
  949.             }
  950.             if (resized) {
  951.             invalidate();
  952.         
  953.                         if (componentListener != null ||
  954.                            (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  955.                             ComponentEvent e = new ComponentEvent(this, 
  956.                                      ComponentEvent.COMPONENT_RESIZED);
  957.                             Toolkit.getEventQueue().postEvent(e);
  958.                         }
  959.             }
  960.                     if (moved && 
  961.                         (componentListener != null ||
  962.                          (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0)) {
  963.                             ComponentEvent e = new ComponentEvent(this, 
  964.                                      ComponentEvent.COMPONENT_MOVED);
  965.                             Toolkit.getEventQueue().postEvent(e);
  966.                     }
  967.             if (parent != null && parent.valid) {
  968.             parent.invalidate();
  969.             }
  970.         }
  971.                 if (isLightweight && isShowing()) {
  972.                     // Repaint the old area ...
  973.                     parent.repaint(oldParentX, oldParentY, oldWidth, oldHeight);
  974.                     // ... then the new (this areas will be collapsed by
  975.                     // the ScreenUpdater if they intersect).
  976.                     repaint();
  977.                 }
  978.         }
  979.     }
  980.     }
  981.  
  982.     /** 
  983.      * Moves and resizes this component to conform to the new 
  984.      * bounding rectangle <code>r</code>. This component's new 
  985.      * position is specified by <code>r.x</code> and <code>r.y</code>, 
  986.      * and its new size is specified by <code>r.width</code> and 
  987.      * <code>r.height</code>
  988.      * @param <code>r<code> The new bounding rectangle for this component.
  989.      * @see       java.awt.Component#getBounds
  990.      * @see       java.awt.Component#setLocation(int, int)
  991.      * @see       java.awt.Component#setLocation(java.awt.Point)
  992.      * @see       java.awt.Component#setSize(int, int)
  993.      * @see       java.awt.Component#setSize(java.awt.Dimension)
  994.      * @since     JDK1.1
  995.      */
  996.     public void setBounds(Rectangle r) {
  997.         setBounds(r.x, r.y, r.width, r.height);
  998.     }
  999.  
  1000.     /** 
  1001.      * Gets the preferred size of this component.
  1002.      * @return A dimension object indicating this component's preferred size.
  1003.      * @see #getMinimumSize
  1004.      * @see java.awt.LayoutManager
  1005.      */
  1006.     public Dimension getPreferredSize() {
  1007.     return preferredSize();
  1008.     }
  1009.  
  1010.     /**
  1011.      * @deprecated As of JDK version 1.1,
  1012.      * replaced by <code>getPreferredSize()</code>.
  1013.      */
  1014.     public Dimension preferredSize() {
  1015.     /* Avoid grabbing the lock if a reasonable cached size value
  1016.          * is available.
  1017.      */
  1018.  
  1019.         Dimension dim = prefSize;
  1020.         if (dim != null && isValid()) {
  1021.         return dim;
  1022.     }
  1023.         
  1024.     synchronized (getTreeLock()) {
  1025.         prefSize = (peer != null) ?
  1026.                peer.preferredSize() :
  1027.                getMinimumSize();
  1028.         return prefSize;
  1029.     }
  1030.     }
  1031.  
  1032.     /**
  1033.      * Gets the mininimum size of this component.
  1034.      * @return A dimension object indicating this component's minimum size.
  1035.      * @see #getPreferredSize
  1036.      * @see java.awtLayoutManager
  1037.      */
  1038.     public Dimension getMinimumSize() {
  1039.           return minimumSize();
  1040.     }
  1041.  
  1042.     /**
  1043.      * @deprecated As of JDK version 1.1,
  1044.      * replaced by <code>getMinimumSize()</code>.
  1045.      */
  1046.     public Dimension minimumSize() {
  1047.     /* Avoid grabbing the lock if a reasonable cached size value
  1048.          * is available.
  1049.      */
  1050.         Dimension dim = minSize;
  1051.         if (dim != null && isValid()) {
  1052.         return dim;
  1053.     }
  1054.     synchronized (getTreeLock()) {
  1055.         minSize = (peer != null) ?
  1056.               peer.minimumSize() :
  1057.               size();
  1058.         return minSize;
  1059.     }
  1060.     }
  1061.  
  1062.     /** 
  1063.      * Gets the maximum size of this component.
  1064.      * @return A dimension object indicating this component's maximum size.
  1065.      * @see #getMinimumSize
  1066.      * @see #getPreferredSize
  1067.      * @see LayoutManager
  1068.      */
  1069.     public Dimension getMaximumSize() {
  1070.     return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  1071.     }
  1072.  
  1073.     /**
  1074.      * Returns the alignment along the x axis.  This specifies how
  1075.      * the component would like to be aligned relative to other 
  1076.      * components.  The value should be a number between 0 and 1
  1077.      * where 0 represents alignment along the origin, 1 is aligned
  1078.      * the furthest away from the origin, 0.5 is centered, etc.
  1079.      */
  1080.     public float getAlignmentX() {
  1081.     return CENTER_ALIGNMENT;
  1082.     }
  1083.  
  1084.     /**
  1085.      * Returns the alignment along the y axis.  This specifies how
  1086.      * the component would like to be aligned relative to other 
  1087.      * components.  The value should be a number between 0 and 1
  1088.      * where 0 represents alignment along the origin, 1 is aligned
  1089.      * the furthest away from the origin, 0.5 is centered, etc.
  1090.      */
  1091.     public float getAlignmentY() {
  1092.     return CENTER_ALIGNMENT;
  1093.     }
  1094.  
  1095.     /**
  1096.      * Prompts the layout manager to lay out this component. This is 
  1097.      * usually called when the component (more specifically, container) 
  1098.      * is validated.
  1099.      * @see #validate
  1100.      * @see LayoutManager
  1101.      */
  1102.     public void doLayout() {
  1103.         layout();
  1104.     }
  1105.  
  1106.     /**
  1107.      * @deprecated As of JDK version 1.1,
  1108.      * replaced by <code>doLayout()</code>.
  1109.      */
  1110.     public void layout() {
  1111.     }
  1112.  
  1113.     /** 
  1114.      * Ensures that this component has a valid layout.  This method is
  1115.      * primarily intended to operate on instances of <code>Container</code>.
  1116.      * @see       java.awt.Component#invalidate      
  1117.      * @see       java.awt.Component#doLayout()
  1118.      * @see       java.awt.LayoutManager
  1119.      * @see       java.awt.Container#validate  
  1120.      * @since     JDK1.0
  1121.      */
  1122.     public void validate() {
  1123.     if (!valid) {
  1124.         synchronized (getTreeLock()) {
  1125.         valid = true;
  1126.         }
  1127.     }
  1128.     }
  1129.  
  1130.     /** 
  1131.      * Invalidates this component.  This component and all parents
  1132.      * above it are marked as needing to be laid out.  This method can
  1133.      * be called often, so it needs to execute quickly.
  1134.      * @see       java.awt.Component#validate
  1135.      * @see       java.awt.Component#doLayout
  1136.      * @see       java.awt.LayoutManager
  1137.      * @since     JDK1.0
  1138.      */
  1139.     public void invalidate() {
  1140.     synchronized (getTreeLock()) {
  1141.         /* Nullify cached layout and size information.
  1142.          * For efficiency, propagate invalidate() upwards only if
  1143.          * some other component hasn't already done so first.
  1144.              */
  1145.         valid = false;
  1146.             prefSize = null;
  1147.             minSize = null;
  1148.         if (parent != null && parent.valid) {
  1149.         parent.invalidate();
  1150.         }
  1151.     }
  1152.     }
  1153.  
  1154.     /**
  1155.      * Creates a graphics context for this component. This method will
  1156.      * return <code>null</code> if this component is currently not on 
  1157.      * the screen.
  1158.      * @return A graphics context for this component, or <code>null</code>
  1159.      *             if it has none.
  1160.      * @see       java.awt.Component#paint
  1161.      * @since     JDK1.0
  1162.      */
  1163.     public Graphics getGraphics() {
  1164.     if (peer instanceof java.awt.peer.LightweightPeer) {
  1165.         // This is for a lightweight component, need to 
  1166.         // translate coordinate spaces and clip relative
  1167.         // to the parent.
  1168.         Graphics g = parent.getGraphics();
  1169.         g.translate(x,y);
  1170.         g.setClip(0, 0, width, height);
  1171.             g.setFont(getFont());
  1172.         return g;
  1173.     } else {
  1174.         ComponentPeer peer = this.peer;
  1175.         return (peer != null) ? peer.getGraphics() : null;
  1176.     }
  1177.     }
  1178.  
  1179.     /**
  1180.      * Gets the font metrics for the specified font.
  1181.      * @param <code>font</code> The font for which font metrics is to be 
  1182.      * obtained.
  1183.      * @return The font metrics for <code>font</code>.
  1184.      * @param     font   the font.
  1185.      * @return    the font metrics for the specified font.
  1186.      * @see       java.awt.Component#getFont
  1187.      * @see       java.awt.Component#getPeer()
  1188.      * @see       java.awt.peer.ComponentPeer#getFontMetrics(java.awt.Font)
  1189.      * @see       java.awt.Toolkit#getFontMetrics(java.awt.Font)
  1190.      * @since     JDK1.0
  1191.      */
  1192.     public FontMetrics getFontMetrics(Font font) {
  1193.         ComponentPeer peer = this.peer;
  1194.     if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  1195.         return peer.getFontMetrics(font);
  1196.     }
  1197.         return getToolkit().getFontMetrics(font);
  1198.     }
  1199.  
  1200.     /**
  1201.      * Set the cursor image to a predefined cursor.
  1202.      * @param <code>cursor</code> One of the constants defined 
  1203.      *            by the <code>Cursor</code> class.
  1204.      * @see       java.awt.Component#getCursor
  1205.      * @see       java.awt.Cursor
  1206.      * @since     JDK1.1
  1207.      */
  1208.     public synchronized void setCursor(Cursor cursor) {
  1209.     this.cursor = cursor;
  1210.         ComponentPeer peer = this.peer;
  1211.     if (peer != null) {
  1212.         peer.setCursor(cursor);
  1213.     }
  1214.     }
  1215.  
  1216.     /**
  1217.      * Gets the cursor set on this component.
  1218.      * @return     The cursor for this component.
  1219.      * @see        java.awt.Component#setCursor
  1220.      * @see        java.awt.Cursor
  1221.      * @since      JDK1.1
  1222.      */
  1223.     public Cursor getCursor() {
  1224.     return cursor;
  1225.     }
  1226.  
  1227.     /** 
  1228.      * Paints this component.  This method is called when the contents
  1229.      * of the component should be painted in response to the component
  1230.      * first being shown or damage needing repair.  The clip rectangle
  1231.      * in the Graphics parameter will be set to the area which needs
  1232.      * to be painted.
  1233.      * @param <code>g</code> The graphics context to use for painting.
  1234.      * @see       java.awt.Component#update
  1235.      * @since     JDK1.0
  1236.      */
  1237.     public void paint(Graphics g) {
  1238.     }
  1239.  
  1240.     /** 
  1241.      * Updates this component. 
  1242.      * <p>
  1243.      * The AWT calls the <code>update</code> method in response to a 
  1244.      * call to <code>repaint</code. The appearance of the 
  1245.      * component on the screen has not changed since the last call to 
  1246.      * <code>update</code> or <code>paint</code>. You can assume that
  1247.      * the background is not cleared.  
  1248.      * <p>
  1249.      * The <code>update</code>method of <code>Component</code>
  1250.      * does the following: 
  1251.      * <p>
  1252.      * <blockquote><ul>
  1253.      * <li>Clears this component by filling it 
  1254.      *      with the background color.
  1255.      * <li>Sets the color of the graphics context to be 
  1256.      *     the foreground color of this component.
  1257.      * <li>Calls this component's <code>paint</code> 
  1258.      *     method to completely redraw this component.
  1259.      * </ul></blockquote>
  1260.      * <p>
  1261.      * The origin of the graphics context, its 
  1262.      * (<code>0</code>, <code>0</code>) coordinate point, is the 
  1263.      * top-left corner of this component. The clipping region of the 
  1264.      * graphics context is the bounding rectangle of this component. 
  1265.      * @param g the specified context to use for updating.
  1266.      * @see       java.awt.Component#paint
  1267.      * @see       java.awt.Component#repaint()
  1268.      * @since     JDK1.0
  1269.      */
  1270.     public void update(Graphics g) {
  1271.     if ((! (peer instanceof java.awt.peer.LightweightPeer)) && 
  1272.               (! (this instanceof Label)) && (! (this instanceof TextField))) {
  1273.         g.setColor(getBackground());
  1274.         g.fillRect(0, 0, width, height);
  1275.         g.setColor(getForeground());
  1276.     }
  1277.     paint(g);
  1278.     }
  1279.  
  1280.     /**
  1281.      * Paints this component and all of its subcomponents. 
  1282.      * <p>
  1283.      * The origin of the graphics context, its 
  1284.      * (<code>0</code>, <code>0</code>) coordinate point, is the 
  1285.      * top-left corner of this component. The clipping region of the 
  1286.      * graphics context is the bounding rectangle of this component. 
  1287.      * @param     g   the graphics context to use for painting.
  1288.      * @see       java.awt.Component#paint
  1289.      * @since     JDK1.0
  1290.      */
  1291.     public void paintAll(Graphics g) {
  1292.     ComponentPeer peer = this.peer;
  1293.     if (visible && (peer != null)) {
  1294.         validate();
  1295.         if (peer instanceof java.awt.peer.LightweightPeer) {
  1296.         paint(g);
  1297.         } else {
  1298.         peer.paint(g);
  1299.         }
  1300.     }
  1301.     }
  1302.  
  1303.     /** 
  1304.      * Repaints this component. 
  1305.      * <p>
  1306.      * This method causes a call to this component's <code>update</code> 
  1307.      * method as soon as possible. 
  1308.      * @see       java.awt.Component#update(java.awt.Graphics)
  1309.      * @since     JDK1.0
  1310.      */
  1311.     public void repaint() {
  1312.     repaint(0, 0, 0, width, height);
  1313.     }
  1314.  
  1315.     /** 
  1316.      * Repaints the component. This will result in a
  1317.      * call to <code>update</code> within <em>tm</em> milliseconds.
  1318.      * @param tm maximum time in milliseconds before update
  1319.      * @see #paint
  1320.      * @see java.awt.Component#update(java.awt.Graphics)
  1321.      * @since JDK1.0
  1322.      */
  1323.     public void repaint(long tm) {
  1324.     repaint(tm, 0, 0, width, height);
  1325.     }
  1326.  
  1327.     /** 
  1328.      * Repaints the specified rectangle of this component. 
  1329.      * <p>
  1330.      * This method causes a call to this component's <code>update</code> 
  1331.      * method as soon as possible. 
  1332.      * @param     x   the <i>x</i> coordinate.
  1333.      * @param     y   the <i>y</i> coordinate.
  1334.      * @param     width   the width.
  1335.      * @param     height  the height.
  1336.      * @see       java.awt.Component#update(java.awt.Graphics)
  1337.      * @since     JDK1.0
  1338.      */
  1339.     public void repaint(int x, int y, int width, int height) {
  1340.     repaint(0, x, y, width, height);
  1341.     }
  1342.  
  1343.     /** 
  1344.      * Repaints the specified rectangle of this component within 
  1345.      * <code>tm</code> milliseconds. 
  1346.      * <p>
  1347.      * This method causes a call to this component's 
  1348.      * <code>update</code> method. 
  1349.      * @param     tm   maximum time in milliseconds before update.
  1350.      * @param     x    the <i>x</i> coordinate.
  1351.      * @param     y    the <i>y</i> coordinate.
  1352.      * @param     width    the width.
  1353.      * @param     height   the height.
  1354.      * @see       java.awt.Component#update(java.awt.Graphics)
  1355.      * @since     JDK1.0
  1356.      */
  1357.     public void repaint(long tm, int x, int y, int width, int height) {
  1358.     if (this.peer instanceof java.awt.peer.LightweightPeer) {
  1359.         // Needs to be translated to parent coordinates since
  1360.         // a parent native container provides the actual repaint
  1361.         // services.  Additionally, the request is restricted to
  1362.         // the bounds of the component.
  1363.         int px = this.x + ((x < 0) ? 0 : x);
  1364.         int py = this.y + ((y < 0) ? 0 : y);
  1365.         int pwidth = (width > this.width) ? this.width : width;
  1366.         int pheight = (height > this.height) ? this.height : height;
  1367.         parent.repaint(tm, px, py, pwidth, pheight);
  1368.     } else {
  1369.         ComponentPeer peer = this.peer;
  1370.         if ((peer != null) && (width > 0) && (height > 0)) {
  1371.         peer.repaint(tm, x, y, width, height);
  1372.         }
  1373.     }
  1374.     }
  1375.  
  1376.     /**
  1377.      * Prints this component. Applications should override this method 
  1378.      * for components that must do special processing before being 
  1379.      * printed or should be printed differently than they are painted. 
  1380.      * <p>
  1381.      * The default implementation of this method calls the 
  1382.      * <code>paint</code> method. 
  1383.      * <p>
  1384.      * The origin of the graphics context, its 
  1385.      * (<code>0</code>, <code>0</code>) coordinate point, is the 
  1386.      * top-left corner of this component. The clipping region of the 
  1387.      * graphics context is the bounding rectangle of this component. 
  1388.      * @param     g   the graphics context to use for printing.
  1389.      * @see       java.awt.Component#paint(java.awt.Graphics)
  1390.      * @since     JDK1.0
  1391.      */
  1392.     public void print(Graphics g) {
  1393.     paint(g);
  1394.     }
  1395.  
  1396.     /**
  1397.      * Prints this component and all of its subcomponents. 
  1398.      * <p>
  1399.      * The origin of the graphics context, its 
  1400.      * (<code>0</code>, <code>0</code>) coordinate point, is the 
  1401.      * top-left corner of this component. The clipping region of the 
  1402.      * graphics context is the bounding rectangle of this component. 
  1403.      * @param     g   the graphics context to use for printing.
  1404.      * @see       java.awt.Component#print(java.awt.Graphics)
  1405.      * @since     JDK1.0
  1406.      */
  1407.     public void printAll(Graphics g) {
  1408.     ComponentPeer peer = this.peer;
  1409.     if (visible && (peer != null)) {
  1410.         validate();
  1411.         Graphics cg = g.create(0, 0, width, height);
  1412.         cg.setFont(getFont());
  1413.         try {
  1414.                 if (peer instanceof java.awt.peer.LightweightPeer) {
  1415.                     lightweightPrint(g);
  1416.                 }
  1417.                 else {
  1418.                     peer.print(g);
  1419.                 }
  1420.         } finally {
  1421.             cg.dispose();
  1422.         }
  1423.     }
  1424.     }
  1425.  
  1426.     /**
  1427.      * Simulates the peer callbacks into java.awt for printing of
  1428.      * lightweight Components.
  1429.      * @param     g   the graphics context to use for printing.
  1430.      * @see       #printAll
  1431.      */
  1432.     void lightweightPrint(Graphics g) {
  1433.         print(g);
  1434.     }
  1435.  
  1436.     /**
  1437.      * Repaints the component when the image has changed.
  1438.      * This <code>imageUpdate</code> method of an <code>ImageObserver</code> 
  1439.      * is called when more information about an 
  1440.      * image which had been previously requested using an asynchronous 
  1441.      * routine such as the <code>drawImage</code> method of 
  1442.      * <code>Graphics</code> becomes available. 
  1443.      * See the definition of <code>imageUpdate</code> for 
  1444.      * more information on this method and its arguments. 
  1445.      * <p>
  1446.      * The <code>imageUpdate</code> method of <code>Component</code> 
  1447.      * incrementally draws an image on the component as more of the bits 
  1448.      * of the image are available. 
  1449.      * <p>
  1450.      * If the system property <code>awt.image.incrementalDraw</code> 
  1451.      * is missing or has the value <code>true</code>, the image is 
  1452.      * incrementally drawn, If the system property has any other value, 
  1453.      * then the image is not drawn until it has been completely loaded. 
  1454.      * <p>
  1455.      * Also, if incremental drawing is in effect, the value of the 
  1456.      * system property <code>awt.image.redrawrate</code> is interpreted 
  1457.      * as an integer to give the maximum redraw rate, in milliseconds. If 
  1458.      * the system property is missing or cannot be interpreted as an 
  1459.      * integer, the redraw rate is once every 100ms. 
  1460.      * <p>
  1461.      * The interpretation of the <code>x</code>, <code>y</code>, 
  1462.      * <code>width</code>, and <code>height</code> arguments depends on 
  1463.      * the value of the <code>infoflags</code> argument. 
  1464.      * @param     img   the image being observed.
  1465.      * @param     infoflags   see <code>imageUpdate</code> for more information.
  1466.      * @param     x   the <i>x</i> coordinate.
  1467.      * @param     y   the <i>y</i> coordinate.
  1468.      * @param     width    the width.
  1469.      * @param     height   the height.
  1470.      * @return    <code>true</code> if the flags indicate that the 
  1471.      *            image is completely loaded; 
  1472.      *            <code>false</code> otherwise.     
  1473.      * @see     java.awt.image.ImageObserver
  1474.      * @see     java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.Color, java.awt.image.ImageObserver)
  1475.      * @see     java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1476.      * @see     java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
  1477.      * @see     java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.image.ImageObserver)
  1478.      * @see     java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  1479.      * @since   JDK1.0
  1480.      */
  1481.     public boolean imageUpdate(Image img, int flags,
  1482.                    int x, int y, int w, int h) {
  1483.     int rate = -1;
  1484.     if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
  1485.         rate = 0;
  1486.     } else if ((flags & SOMEBITS) != 0) {
  1487.         if (isInc) {
  1488.         try {
  1489.             rate = incRate;
  1490.             if (rate < 0)
  1491.             rate = 0;
  1492.         } catch (Exception e) {
  1493.             rate = 100;
  1494.         }
  1495.         }
  1496.     }
  1497.     if (rate >= 0) {
  1498.         repaint(rate, 0, 0, width, height);
  1499.     }
  1500.     return (flags & (ALLBITS|ABORT)) == 0;
  1501.     }
  1502.  
  1503.     /**
  1504.      * Creates an image from the specified image producer.
  1505.      * @param     producer  the image producer
  1506.      * @return    the image produced.    
  1507.      * @since     JDK1.0
  1508.      */
  1509.     public Image createImage(ImageProducer producer) {
  1510.         ComponentPeer peer = this.peer;
  1511.     if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  1512.         return peer.createImage(producer);
  1513.     }
  1514.     return getToolkit().createImage(producer);
  1515.     }
  1516.  
  1517.     /**
  1518.      * Creates an off-screen drawable image 
  1519.      *     to be used for double buffering.
  1520.      * @param     width the specified width.
  1521.      * @param     height the specified height.
  1522.      * @return    an off-screen drawable image, 
  1523.      *            which can be used for double buffering.
  1524.      * @since     JDK1.0
  1525.      */
  1526.     public Image createImage(int width, int height) {
  1527.         ComponentPeer peer = this.peer;
  1528.     if (peer instanceof java.awt.peer.LightweightPeer) {
  1529.         return parent.createImage(width, height);
  1530.     } else {
  1531.         return (peer != null) ? peer.createImage(width, height) : null;
  1532.     }
  1533.     }
  1534.  
  1535.     /**
  1536.      * Prepares an image for rendering on this component.  The image
  1537.      * data is downloaded asynchronously in another thread and the
  1538.      * appropriate screen representation of the image is generated.
  1539.      * @param     image   the <code>Image</code> for which to 
  1540.      *                    prepare a screen representation.
  1541.      * @param     observer   the <code>ImageObserver</code> object 
  1542.      *                       to be notified as the image is being prepared.
  1543.      * @return    <code>true</code> if the image has already been fully prepared; 
  1544.                   <code>false</code> otherwise.     
  1545.      * @since     JDK1.0
  1546.      */
  1547.     public boolean prepareImage(Image image, ImageObserver observer) {
  1548.         return prepareImage(image, -1, -1, observer);
  1549.     }
  1550.  
  1551.     /**
  1552.      * Prepares an image for rendering on this component at the 
  1553.      * specified width and height. 
  1554.      * <p>
  1555.      * The image data is downloaded asynchronously in another thread, 
  1556.      * and an appropriately scaled screen representation of the image is 
  1557.      * generated. 
  1558.      * @param     image    the instance of <code>Image</code> 
  1559.      *            for which to prepare a screen representation.
  1560.      * @param     width    the width of the desired screen representation.
  1561.      * @param     height   the height of the desired screen representation.
  1562.      * @param     observer   the <code>ImageObserver</code> object 
  1563.      *            to be notified as the image is being prepared.
  1564.      * @return    <code>true</code> if the image has already been fully prepared; 
  1565.                   <code>false</code> otherwise.
  1566.      * @see       java.awt.image.ImageObserver     
  1567.      * @since     JDK1.0
  1568.      */
  1569.     public boolean prepareImage(Image image, int width, int height,
  1570.                 ImageObserver observer) {
  1571.         ComponentPeer peer = this.peer;
  1572.     if (peer instanceof java.awt.peer.LightweightPeer) {
  1573.         return parent.prepareImage(image, width, height, observer);
  1574.     } else {
  1575.         return (peer != null)
  1576.         ? peer.prepareImage(image, width, height, observer)
  1577.         : getToolkit().prepareImage(image, width, height, observer);
  1578.     }
  1579.     }
  1580.  
  1581.     /**
  1582.      * Returns the status of the construction of a screen representation 
  1583.      * of the specified image. 
  1584.      * <p>
  1585.      * This method does not cause the image to begin loading. An 
  1586.      * application must use the <code>prepareImage</code> method 
  1587.      * to force the loading of an image. 
  1588.      * <p>
  1589.      * Information on the flags returned by this method can be found 
  1590.      * with the discussion of the <code>ImageObserver</code> interface. 
  1591.      * @param     image   the <code>Image</code> object whose status 
  1592.      *            is being checked.
  1593.      * @param     observer   the <code>ImageObserver</code> 
  1594.      *            object to be notified as the image is being prepared.
  1595.      * @return  the bitwise inclusive <b>OR</b> of 
  1596.      *            <code>ImageObserver</code> flags indicating what 
  1597.      *            information about the image is currently available.     
  1598.      * @see      java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1599.      * @see      java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1600.      * @see      java.awt.image.ImageObserver
  1601.      * @since    JDK1.0
  1602.      */
  1603.     public int checkImage(Image image, ImageObserver observer) {
  1604.         return checkImage(image, -1, -1, observer);
  1605.     }
  1606.  
  1607.     /**
  1608.      * Returns the status of the construction of a screen representation 
  1609.      * of the specified image. 
  1610.      * <p>
  1611.      * This method does not cause the image to begin loading. An 
  1612.      * application must use the <code>prepareImage</code> method 
  1613.      * to force the loading of an image. 
  1614.      * <p>
  1615.      * The <code>checkImage</code> method of <code>Component</code> 
  1616.      * calls its peer's <code>checkImage</code> method to calculate 
  1617.      * the flags. If this component does not yet have a peer, the 
  1618.      * component's toolkit's <code>checkImage</code> method is called 
  1619.      * instead. 
  1620.      * <p>
  1621.      * Information on the flags returned by this method can be found 
  1622.      * with the discussion of the <code>ImageObserver</code> interface. 
  1623.      * @param     image   the <code>Image</code> object whose status 
  1624.      *                    is being checked.
  1625.      * @param     width   the width of the scaled version 
  1626.      *                    whose status is to be checked.
  1627.      * @param     height  the height of the scaled version 
  1628.      *                    whose status is to be checked.
  1629.      * @param     observer   the <code>ImageObserver</code> object 
  1630.      *                    to be notified as the image is being prepared.
  1631.      * @return    the bitwise inclusive <b>OR</b> of 
  1632.      *            <code>ImageObserver</code> flags indicating what 
  1633.      *            information about the image is currently available.     
  1634.      * @see      java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1635.      * @see      java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
  1636.      * @see      java.awt.image.ImageObserver#_top_
  1637.      * @since    JDK1.0
  1638.      */
  1639.     public int checkImage(Image image, int width, int height,
  1640.               ImageObserver observer) {
  1641.         ComponentPeer peer = this.peer;
  1642.     if (peer instanceof java.awt.peer.LightweightPeer) {
  1643.         return parent.checkImage(image, width, height, observer);
  1644.     } else {
  1645.         return (peer != null)
  1646.         ? peer.checkImage(image, width, height, observer)
  1647.         : getToolkit().checkImage(image, width, height, observer);
  1648.     }
  1649.     }
  1650.  
  1651.     /**  
  1652.      * Checks whether this component "contains" the specified point,
  1653.      * where <code>x</code> and <code>y</code> are defined to be 
  1654.      * relative to the coordinate system of this component.
  1655.      * @param     x   the <i>x</i> coordinate of the point.
  1656.      * @param     y   the <i>y</i> coordinate of the point.
  1657.      * @see       java.awt.Component#getComponentAt(int, int)
  1658.      * @since     JDK1.1
  1659.      */
  1660.     public boolean contains(int x, int y) {
  1661.         return inside(x, y);
  1662.     }
  1663.  
  1664.     /** 
  1665.      * @deprecated As of JDK version 1.1, 
  1666.      * replaced by contains(int, int).
  1667.      */
  1668.     public boolean inside(int x, int y) {
  1669.     return (x >= 0) && (x < width) && (y >= 0) && (y < height);
  1670.     }
  1671.  
  1672.     /**  
  1673.      * Checks whether this component "contains" the specified point,
  1674.      * where the point's <i>x</i> and <i>y</i> coordinates are defined 
  1675.      * to be relative to the coordinate system of this component.  
  1676.      * @param     p     the point.
  1677.      * @see       java.awt.Component#getComponentAt(java.awt.Point)
  1678.      * @since     JDK1.1
  1679.      */
  1680.     public boolean contains(Point p) {
  1681.     return contains(p.x, p.y);
  1682.     }
  1683.  
  1684.     /** 
  1685.      * Determines if this component or one of its immediate 
  1686.      * subcomponents contains the (<i>x</i>, <i>y</i>) location, 
  1687.      * and if so, returns the containing component. This method only 
  1688.      * looks one level deep. If the point (<i>x</i>, <i>y</i>) is 
  1689.      * inside a subcomponent that itself has subcomponents, it does not 
  1690.      * go looking down the subcomponent tree. 
  1691.      * <p>
  1692.      * The <code>locate</code> method of <code>Component</code> simply 
  1693.      * returns the component itself if the (<i>x</i>, <i>y</i>) 
  1694.      * coordinate location is inside its bounding box, and <code>null</code> 
  1695.      * otherwise. 
  1696.      * @param     x   the <i>x</i> coordinate.
  1697.      * @param     y   the <i>y</i> coordinate.
  1698.      * @return    the component or subcomponent that contains the 
  1699.      *                (<i>x</i>, <i>y</i>) location; 
  1700.      *                <code>null</code> if the location 
  1701.      *                is outside this component.
  1702.      * @see       java.awt.Component#contains(int, int)
  1703.      * @since     JDK1.0
  1704.      */
  1705.     public Component getComponentAt(int x, int y) {
  1706.     return locate(x, y);
  1707.     }
  1708.  
  1709.     /** 
  1710.      * @deprecated As of JDK version 1.1,
  1711.      * replaced by getComponentAt(int, int).
  1712.      */
  1713.     public Component locate(int x, int y) {
  1714.     return contains(x, y) ? this : null;
  1715.     }
  1716.  
  1717.     /** 
  1718.      * Returns the component or subcomponent that contains the
  1719.      * specified point.
  1720.      * @param     p   the point.
  1721.      * @see       java.awt.Component#contains
  1722.      * @since     JDK1.1
  1723.      */
  1724.     public Component getComponentAt(Point p) {
  1725.     return getComponentAt(p.x, p.y);
  1726.     }
  1727.  
  1728.     /**
  1729.      * @deprecated As of JDK version 1.1,
  1730.      * replaced by <code>dispatchEvent(AWTEvent e)</code>.
  1731.      */
  1732.     public void deliverEvent(Event e) {
  1733.     postEvent(e);
  1734.     }
  1735.  
  1736.     /**
  1737.      * Dispatches an event to this component or one of its sub components.
  1738.      * @param e the event
  1739.      */
  1740.     public final void dispatchEvent(AWTEvent e) {
  1741.         dispatchEventImpl(e);
  1742.     }
  1743.  
  1744.     void dispatchEventImpl(AWTEvent e) {
  1745.         int id = e.getID();
  1746.         /*
  1747.      * 1. Allow input methods to process the event
  1748.      */
  1749.     if (areInputMethodsEnabled()
  1750.             && (
  1751.                 // Otherwise, we only pass on low-level events, because
  1752.                 // a) input methods shouldn't know about semantic events
  1753.                 // b) passing on the events takes time
  1754.                 // c) isConsumed() is always true for semantic events.
  1755.                 // We exclude paint events since they may be numerous and shouldn't matter.
  1756.                 (e instanceof ComponentEvent) && !(e instanceof PaintEvent))) {
  1757.             InputContext inputContext = getInputContext();
  1758.             if (inputContext != null) {
  1759.                 inputContext.dispatchEvent(e);
  1760.             if (e.isConsumed()) {
  1761.                 return;
  1762.             }
  1763.         }
  1764.         }
  1765.  
  1766.         /*
  1767.          * 2. Pre-process any special events before delivery
  1768.          */
  1769.         switch(id) {
  1770.       // Handling of the PAINT and UPDATE events is now done in the
  1771.       // peer's handleEvent() method so the background can be cleared
  1772.       // selectively for non-native components on Windows only.
  1773.       // - Fred.Ecks@Eng.sun.com, 1-8-98
  1774.  
  1775.           case FocusEvent.FOCUS_GAINED:
  1776.             if (parent != null && !(this instanceof Window)) {
  1777.                 parent.setFocusOwner(this);
  1778.             }
  1779.             break;
  1780.  
  1781.           case KeyEvent.KEY_PRESSED:
  1782.           case KeyEvent.KEY_RELEASED:
  1783.             Container p = (Container)((this instanceof Container) ? this : parent);
  1784.             if (p != null) {
  1785.                 p.preProcessKeyEvent((KeyEvent)e);
  1786.                 if (e.isConsumed()) {
  1787.                     return;
  1788.                 }
  1789.             }
  1790.             break;
  1791.  
  1792.           case MouseEvent.MOUSE_PRESSED:
  1793. //              requestFocus();
  1794.               break;
  1795.  
  1796.           default:
  1797.             break;
  1798.         }
  1799.  
  1800.         /*
  1801.          * 3. Deliver event for normal processing
  1802.          */
  1803.         if (newEventsOnly) {
  1804.             // Filtering needs to really be moved to happen at a lower
  1805.             // level in order to get maximum performance gain;  it is
  1806.             // here temporarily to ensure the API spec is honored.
  1807.             //
  1808.             if (eventEnabled(e)) {
  1809.                 processEvent(e);
  1810.             }
  1811.  
  1812.         } else if (!(e instanceof MouseEvent && !postsOldMouseEvents())) { 
  1813.             //
  1814.             // backward compatibility
  1815.             //
  1816.             Event olde = e.convertToOld();
  1817.             if (olde != null) {
  1818.                 int key = olde.key;
  1819.                 int modifiers = olde.modifiers;
  1820.  
  1821.                 postEvent(olde);
  1822.                 if (olde.isConsumed()) {
  1823.                     e.consume();
  1824.                 }
  1825.                 // if target changed key or modifier values, copy them
  1826.                 // back to original event
  1827.                 //
  1828.                 switch(olde.id) {
  1829.                   case Event.KEY_PRESS:
  1830.                   case Event.KEY_RELEASE:
  1831.                   case Event.KEY_ACTION:
  1832.                   case Event.KEY_ACTION_RELEASE:
  1833.                     if (olde.key != key) {
  1834.                        ((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
  1835.                     }
  1836.                     if (olde.modifiers != modifiers) {
  1837.                        ((KeyEvent)e).setModifiers(olde.modifiers);
  1838.                     }
  1839.                     break;
  1840.                   default:
  1841.                     break;
  1842.                 }
  1843.             } 
  1844.         }
  1845.  
  1846.         /* 
  1847.          * 4. If no one has consumed a key event, propagate it
  1848.          * up the containment hierarchy to ensure that menu shortcuts
  1849.          * and keyboard traversal will work properly.
  1850.          */
  1851.         if (!e.isConsumed() && e instanceof java.awt.event.KeyEvent) {
  1852.             Container p = (Container)((this instanceof Container) ? this : parent);
  1853.             if (p != null) {
  1854.                 p.postProcessKeyEvent((KeyEvent)e);
  1855.             }
  1856.         }
  1857.  
  1858.         /*
  1859.          * 5. Allow the peer to process the event
  1860.          */
  1861.         if (peer != null) {
  1862.             peer.handleEvent(e);
  1863.         }
  1864.     }
  1865.  
  1866.     boolean areInputMethodsEnabled() {
  1867.         // in 1.1.x, we assume input method support is required for all
  1868.         // components that handle key events. There's no way to tell
  1869.         // whether they're really interested in character input or just
  1870.         // in keystrokes.
  1871.         return (eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null;
  1872.     }
  1873.  
  1874.     // REMIND: remove when filtering is handled at lower level
  1875.     boolean eventEnabled(AWTEvent e) {
  1876.         switch(e.id) {
  1877.           case ComponentEvent.COMPONENT_MOVED:
  1878.           case ComponentEvent.COMPONENT_RESIZED:
  1879.           case ComponentEvent.COMPONENT_SHOWN:
  1880.           case ComponentEvent.COMPONENT_HIDDEN:
  1881.             if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
  1882.                 componentListener != null) {
  1883.                 return true;
  1884.             }
  1885.             break;
  1886.           case FocusEvent.FOCUS_GAINED:
  1887.           case FocusEvent.FOCUS_LOST:
  1888.             if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 ||
  1889.                 focusListener != null) {
  1890.                 return true;
  1891.             }
  1892.             break;
  1893.           case KeyEvent.KEY_PRESSED:
  1894.           case KeyEvent.KEY_RELEASED:
  1895.           case KeyEvent.KEY_TYPED:
  1896.             if ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 ||
  1897.                 keyListener != null) {
  1898.                 return true;
  1899.             }
  1900.             break;
  1901.           case MouseEvent.MOUSE_PRESSED:
  1902.           case MouseEvent.MOUSE_RELEASED:
  1903.           case MouseEvent.MOUSE_ENTERED:
  1904.           case MouseEvent.MOUSE_EXITED:
  1905.           case MouseEvent.MOUSE_CLICKED:
  1906.             if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 ||
  1907.                 mouseListener != null) {
  1908.                 return true;
  1909.             }
  1910.             break;
  1911.           case MouseEvent.MOUSE_MOVED:
  1912.           case MouseEvent.MOUSE_DRAGGED:
  1913.             if ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 ||
  1914.                 mouseMotionListener != null) {
  1915.                 return true;
  1916.             }
  1917.             break;
  1918.           default:
  1919.             break;
  1920.         }
  1921.         //
  1922.         // Always pass on events defined by external programs.
  1923.         //
  1924.         if (e.id > AWTEvent.RESERVED_ID_MAX) {
  1925.             return true;
  1926.         }
  1927.         return false;
  1928.     }
  1929.  
  1930.     /**
  1931.      * @deprecated As of JDK version 1.1,
  1932.      * replaced by dispatchEvent(AWTEvent).
  1933.      */
  1934.     public boolean postEvent(Event e) {
  1935.     ComponentPeer peer = this.peer;
  1936.  
  1937.     if (handleEvent(e)) {
  1938.             e.consume();
  1939.         return true;
  1940.     }
  1941.  
  1942.     Component parent = this.parent;
  1943.     int eventx = e.x;
  1944.     int eventy = e.y;
  1945.     if (parent != null) {
  1946.         e.translate(x, y);
  1947.         if (parent.postEvent(e)) {
  1948.                 e.consume();
  1949.             return true;
  1950.         }
  1951.         // restore coords
  1952.            e.x = eventx;
  1953.         e.y = eventy;
  1954.     }
  1955.     return false;
  1956.     }
  1957.  
  1958.     // Event source interfaces
  1959.  
  1960.     /**
  1961.      * Adds the specified component listener to receive component events from
  1962.      * this component.
  1963.      * @param    l   the component listener.
  1964.      * @see      java.awt.event.ComponentEvent
  1965.      * @see      java.awt.event.ComponentListener
  1966.      * @see      java.awt.Component#removeComponentListener
  1967.      * @since    JDK1.1
  1968.      */  
  1969.     public synchronized void addComponentListener(ComponentListener l) {
  1970.         componentListener = AWTEventMulticaster.add(componentListener, l);
  1971.         newEventsOnly = true;
  1972.     }
  1973.     /**
  1974.      * Removes the specified component listener so that it no longer
  1975.      * receives component events from this component.
  1976.      * @param    l   the component listener.
  1977.      * @see      java.awt.event.ComponentEvent
  1978.      * @see      java.awt.event.ComponentListener
  1979.      * @see      java.awt.Component#addComponentListener
  1980.      * @since    JDK1.1
  1981.      */ 
  1982.     public synchronized void removeComponentListener(ComponentListener l) {
  1983.         componentListener = AWTEventMulticaster.remove(componentListener, l);
  1984.     }        
  1985.  
  1986.     /**
  1987.      * Adds the specified focus listener to receive focus events from
  1988.      * this component.
  1989.      * @param    l   the focus listener.
  1990.      * @see      java.awt.event.FocusEvent
  1991.      * @see      java.awt.event.FocusListener
  1992.      * @see      java.awt.Component#removeFocusListener
  1993.      * @since    JDK1.1
  1994.      */      
  1995.     public synchronized void addFocusListener(FocusListener l) {
  1996.         focusListener = AWTEventMulticaster.add(focusListener, l);
  1997.         newEventsOnly = true;
  1998.  
  1999.     // if this is a lightweight component, enable focus events
  2000.     // in the native container.
  2001.     if (peer instanceof java.awt.peer.LightweightPeer) {
  2002.         parent.proxyEnableEvents(AWTEvent.FOCUS_EVENT_MASK);
  2003.     }
  2004.     }
  2005.  
  2006.     /**
  2007.      * Removes the specified focus listener so that it no longer
  2008.      * receives focus events from this component.
  2009.      * @param    l   the focus listener.
  2010.      * @see      java.awt.event.FocusEvent
  2011.      * @see      java.awt.event.FocusListener
  2012.      * @see      java.awt.Component#addFocusListener
  2013.      * @since    JDK1.1
  2014.      */ 
  2015.     public synchronized void removeFocusListener(FocusListener l) {
  2016.         focusListener = AWTEventMulticaster.remove(focusListener, l);
  2017.     }   
  2018.  
  2019.     /**
  2020.      * Adds the specified key listener to receive key events from
  2021.      * this component.
  2022.      * @param    l   the key listener.
  2023.      * @see      java.awt.event.KeyEvent
  2024.      * @see      java.awt.event.KeyListener
  2025.      * @see      java.awt.Component#removeKeyListener
  2026.      * @since    JDK1.1
  2027.      */      
  2028.     public synchronized void addKeyListener(KeyListener l) {
  2029.         keyListener = AWTEventMulticaster.add(keyListener, l);
  2030.         newEventsOnly = true;
  2031.  
  2032.     // if this is a lightweight component, enable key events
  2033.     // in the native container.
  2034.     if (peer instanceof java.awt.peer.LightweightPeer) {
  2035.         parent.proxyEnableEvents(AWTEvent.KEY_EVENT_MASK);
  2036.     }
  2037.     }
  2038.  
  2039.     /**
  2040.      * Removes the specified key listener so that it no longer
  2041.      * receives key events from this component.
  2042.      * @param    l   the key listener.
  2043.      * @see      java.awt.event.KeyEvent
  2044.      * @see      java.awt.event.KeyListener
  2045.      * @see      java.awt.Component#addKeyListener
  2046.      * @since    JDK1.1
  2047.      */ 
  2048.     public synchronized void removeKeyListener(KeyListener l) {
  2049.         keyListener = AWTEventMulticaster.remove(keyListener, l);
  2050.     }  
  2051.  
  2052.     /**
  2053.      * Adds the specified mouse listener to receive mouse events from
  2054.      * this component.
  2055.      * @param    l   the mouse listener.
  2056.      * @see      java.awt.event.MouseEvent
  2057.      * @see      java.awt.event.MouseListener
  2058.      * @see      java.awt.Component#removeMouseListener
  2059.      * @since    JDK1.1
  2060.      */      
  2061.     public synchronized void addMouseListener(MouseListener l) {
  2062.         mouseListener = AWTEventMulticaster.add(mouseListener,l);
  2063.         newEventsOnly = true;
  2064.     
  2065.     // if this is a lightweight component, enable mouse events
  2066.     // in the native container.
  2067.     if (peer instanceof java.awt.peer.LightweightPeer) {
  2068.         parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
  2069.     }
  2070.     }
  2071.  
  2072.     /**
  2073.      * Removes the specified mouse listener so that it no longer
  2074.      * receives mouse events from this component.
  2075.      * @param    l   the mouse listener.
  2076.      * @see      java.awt.event.MouseEvent
  2077.      * @see      java.awt.event.MouseListener
  2078.      * @see      java.awt.Component#addMouseListener
  2079.      * @since    JDK1.1
  2080.      */ 
  2081.     public synchronized void removeMouseListener(MouseListener l) {
  2082.         mouseListener = AWTEventMulticaster.remove(mouseListener, l);
  2083.     }
  2084.  
  2085.     /**
  2086.      * Adds the specified mouse motion listener to receive mouse motion events from
  2087.      * this component.
  2088.      * @param    l   the mouse motion listener.
  2089.      * @see      java.awt.event.MouseMotionEvent
  2090.      * @see      java.awt.event.MouseMotionListener
  2091.      * @see      java.awt.Component#removeMouseMotionListener
  2092.      * @since    JDK1.1
  2093.      */  
  2094.     public synchronized void addMouseMotionListener(MouseMotionListener l) {
  2095.         mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener,l);
  2096.         newEventsOnly = true;
  2097.     
  2098.     // if this is a lightweight component, enable mouse events
  2099.     // in the native container.
  2100.     if (peer instanceof java.awt.peer.LightweightPeer) {
  2101.         parent.proxyEnableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  2102.     }
  2103.     }
  2104.  
  2105.     /**
  2106.      * Removes the specified mouse motion listener so that it no longer
  2107.      * receives mouse motion events from this component.
  2108.      * @param    l   the mouse motion listener.
  2109.      * @see      java.awt.event.MouseMotionEvent
  2110.      * @see      java.awt.event.MouseMotionListener
  2111.      * @see      java.awt.Component#addMouseMotionListener
  2112.      * @since    JDK1.1
  2113.      */ 
  2114.     public synchronized void removeMouseMotionListener(MouseMotionListener l) {
  2115.         mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
  2116.     }    
  2117.  
  2118.     /**
  2119.      * Gets the input context used by this component for handling the communication
  2120.      * with input methods when text is entered in this component. By default, the
  2121.      * input context used for the parent component is returned. Components may
  2122.      * override this to return a private input context.
  2123.      *
  2124.      * @return The input context used by this component. Null if no context can
  2125.      * be determined.
  2126.      */
  2127.     InputContext getInputContext() {
  2128.         Container parent = this.parent;
  2129.         if (parent == null) {
  2130.             return null;
  2131.         } else {
  2132.             return parent.getInputContext();
  2133.         }
  2134.     }
  2135.  
  2136.     /**
  2137.      * Enables the events defined by the specified event mask parameter
  2138.      * to be delivered to this component. 
  2139.      * <p>
  2140.      * Event types are automatically enabled when a listener for 
  2141.      * that event type is added to the component.
  2142.      * <p>
  2143.      * This method only needs to be invoked by subclasses of
  2144.      * <code>Component</code> which desire to have the specified event  
  2145.      * types delivered to <code>processEvent</code> regardless of whether 
  2146.      * or not a listener is registered.
  2147.      * @param      eventsToEnable   the event mask defining the event types.
  2148.      * @see        java.awt.Component#processEvent
  2149.      * @see        java.awt.Component#disableEvents
  2150.      * @since      JDK1.1
  2151.      */
  2152.     protected final void enableEvents(long eventsToEnable) {
  2153.         eventMask |= eventsToEnable;
  2154.         newEventsOnly = true;
  2155.    
  2156.     // if this is a lightweight component, enable mouse events
  2157.     // in the native container.
  2158.     if (peer instanceof java.awt.peer.LightweightPeer) {
  2159.         parent.proxyEnableEvents(eventMask);
  2160.     }
  2161.     }
  2162.  
  2163.     /**
  2164.      * Disables the events defined by the specified event mask parameter
  2165.      * from being delivered to this component.  
  2166.      * @param      eventsToDisable   the event mask defining the event types
  2167.      * @see        java.awt.Component#enableEvents
  2168.      * @since      JDK1.1
  2169.      */
  2170.     protected final void disableEvents(long eventsToDisable) {
  2171.         eventMask &= ~eventsToDisable;  
  2172.     }  
  2173.  
  2174.     /** 
  2175.      * Processes events occurring on this component. By default this
  2176.      * method calls the appropriate 
  2177.      * <code>process<event type>Event</code>  
  2178.      * method for the given class of event.
  2179.      * @param     e the event.
  2180.      * @see       java.awt.Component#processComponentEvent
  2181.      * @see       java.awt.Component#processFocusEvent
  2182.      * @see       java.awt.Component#processKeyEvent
  2183.      * @see       java.awt.Component#processMouseEvent
  2184.      * @see       java.awt.Component#processMouseMotionEvent
  2185.      * @since     JDK1.1
  2186.      */   
  2187.     protected void processEvent(AWTEvent e) {
  2188.  
  2189.         //System.err.println("Component.processNewEvent:" + e);
  2190.         if (e instanceof FocusEvent) {  
  2191.             processFocusEvent((FocusEvent)e);
  2192.  
  2193.         } else if (e instanceof MouseEvent) {
  2194.             switch(e.getID()) {
  2195.               case MouseEvent.MOUSE_PRESSED:
  2196.               case MouseEvent.MOUSE_RELEASED:
  2197.               case MouseEvent.MOUSE_CLICKED:
  2198.               case MouseEvent.MOUSE_ENTERED:
  2199.               case MouseEvent.MOUSE_EXITED:
  2200.                 processMouseEvent((MouseEvent)e);
  2201.                 break;
  2202.               case MouseEvent.MOUSE_MOVED:
  2203.               case MouseEvent.MOUSE_DRAGGED:
  2204.                 processMouseMotionEvent((MouseEvent)e);
  2205.                 break;
  2206.             }
  2207.  
  2208.         } else if (e instanceof KeyEvent) {
  2209.             processKeyEvent((KeyEvent)e);
  2210.  
  2211.         } else if (e instanceof ComponentEvent) {
  2212.             processComponentEvent((ComponentEvent)e);
  2213.         }
  2214.     }
  2215.     
  2216.     /** 
  2217.      * Processes component events occurring on this component by
  2218.      * dispatching them to any registered 
  2219.      * <code>ComponentListener</code> objects. 
  2220.      * <p>
  2221.      * This method is not called unless component events are 
  2222.      * enabled for this component. Component events are enabled 
  2223.      * when one of the following occurs:
  2224.      * <p><ul>
  2225.      * <li>A <code>ComponentListener</code> object is registered 
  2226.      * via <code>addComponentListener</code>.
  2227.      * <li>Component events are enabled via <code>enableEvents</code>.
  2228.      * </ul>
  2229.      * @param       e the component event.
  2230.      * @see         java.awt.event.ComponentEvent
  2231.      * @see         java.awt.event.ComponentListener
  2232.      * @see         java.awt.Component#addComponentListener
  2233.      * @see         java.awt.Component#enableEvents
  2234.      * @since       JDK1.1
  2235.      */   
  2236.     protected void processComponentEvent(ComponentEvent e) {
  2237.         if (componentListener != null) {
  2238.             int id = e.getID();
  2239.             switch(id) {
  2240.               case ComponentEvent.COMPONENT_RESIZED:
  2241.                 componentListener.componentResized(e);
  2242.                 break;
  2243.               case ComponentEvent.COMPONENT_MOVED:
  2244.                 componentListener.componentMoved(e);
  2245.                 break;
  2246.               case ComponentEvent.COMPONENT_SHOWN:
  2247.                 componentListener.componentShown(e);
  2248.                 break;
  2249.               case ComponentEvent.COMPONENT_HIDDEN:
  2250.                 componentListener.componentHidden(e);
  2251.                 break;
  2252.             }
  2253.         }
  2254.     }
  2255.  
  2256.     /** 
  2257.      * Processes focus events occurring on this component by
  2258.      * dispatching them to any registered 
  2259.      * <code>FocusListener</code> objects. 
  2260.      * <p>
  2261.      * This method is not called unless focus events are 
  2262.      * enabled for this component. Focus events are enabled 
  2263.      * when one of the following occurs:
  2264.      * <p><ul>
  2265.      * <li>A <code>FocusListener</code> object is registered 
  2266.      * via <code>addFocusListener</code>.
  2267.      * <li>Focus events are enabled via <code>enableEvents</code>.
  2268.      * </ul>
  2269.      * @param       e the focus event.
  2270.      * @see         java.awt.event.FocusEvent
  2271.      * @see         java.awt.event.FocusListener
  2272.      * @see         java.awt.Component#addFocusListener
  2273.      * @see         java.awt.Component#enableEvents
  2274.      * @since       JDK1.1
  2275.      */       
  2276.     protected void processFocusEvent(FocusEvent e) {
  2277.         if (focusListener != null) {
  2278.             int id = e.getID();
  2279.             switch(id) {
  2280.               case FocusEvent.FOCUS_GAINED:
  2281.                 focusListener.focusGained(e);
  2282.                 break;
  2283.               case FocusEvent.FOCUS_LOST:
  2284.                 focusListener.focusLost(e);
  2285.                 break;
  2286.             }
  2287.         }
  2288.     }
  2289.  
  2290.     /** 
  2291.      * Processes key events occurring on this component by
  2292.      * dispatching them to any registered 
  2293.      * <codeKeyListener</code> objects. 
  2294.      * <p>
  2295.      * This method is not called unless key events are 
  2296.      * enabled for this component. Key events are enabled 
  2297.      * when one of the following occurs:
  2298.      * <p><ul>
  2299.      * <li>A <code>KeyListener</code> object is registered 
  2300.      * via <code>addKeyListener</code>.
  2301.      * <li>Key events are enabled via <code>enableEvents</code>.
  2302.      * </ul>
  2303.      * @param       e the key event.
  2304.      * @see         java.awt.event.KeyEvent
  2305.      * @see         java.awt.event.KeyListener
  2306.      * @see         java.awt.Component#addKeyListener
  2307.      * @see         java.awt.Component#enableEvents
  2308.      * @since       JDK1.1
  2309.      */   
  2310.     protected void processKeyEvent(KeyEvent e) {
  2311.         if (keyListener != null) {
  2312.             int id = e.getID();
  2313.             switch(id) {
  2314.               case KeyEvent.KEY_TYPED:
  2315.                 keyListener.keyTyped(e);
  2316.                 break;
  2317.               case KeyEvent.KEY_PRESSED:
  2318.                 keyListener.keyPressed(e);
  2319.                 break;
  2320.               case KeyEvent.KEY_RELEASED:
  2321.                 keyListener.keyReleased(e);
  2322.                 break;
  2323.             }
  2324.         } 
  2325.     }
  2326.  
  2327.     /** 
  2328.      * Processes mouse events occurring on this component by
  2329.      * dispatching them to any registered 
  2330.      * <code>MouseListener</code> objects. 
  2331.      * <p>
  2332.      * This method is not called unless mouse events are 
  2333.      * enabled for this component. Mouse events are enabled 
  2334.      * when one of the following occurs:
  2335.      * <p><ul>
  2336.      * <li>A <code>MouseListener</code> object is registered 
  2337.      * via <code>addMouseListener</code>.
  2338.      * <li>Mouse events are enabled via <code>enableEvents</code>.
  2339.      * </ul>
  2340.      * @param       e the mouse event.
  2341.      * @see         java.awt.event.MouseEvent
  2342.      * @see         java.awt.event.MouseListener
  2343.      * @see         java.awt.Component#addMouseListener
  2344.      * @see         java.awt.Component#enableEvents
  2345.      * @since       JDK1.1
  2346.      */   
  2347.     protected void processMouseEvent(MouseEvent e) {
  2348.         if (mouseListener != null) {
  2349.             int id = e.getID();
  2350.             switch(id) {
  2351.               case MouseEvent.MOUSE_PRESSED:
  2352.                 mouseListener.mousePressed(e);
  2353.                 break;
  2354.               case MouseEvent.MOUSE_RELEASED:
  2355.                 mouseListener.mouseReleased(e);
  2356.                 break;
  2357.               case MouseEvent.MOUSE_CLICKED:
  2358.                 mouseListener.mouseClicked(e);
  2359.                 break;
  2360.               case MouseEvent.MOUSE_EXITED:
  2361.                 mouseListener.mouseExited(e);
  2362.                 break;
  2363.               case MouseEvent.MOUSE_ENTERED:
  2364.                 mouseListener.mouseEntered(e);
  2365.                 break;
  2366.             }
  2367.         }            
  2368.     }
  2369.  
  2370.     /** 
  2371.      * Processes mouse motion events occurring on this component by
  2372.      * dispatching them to any registered 
  2373.      * <code>MouseMotionListener</code> objects. 
  2374.      * <p>
  2375.      * This method is not called unless mouse motion events are 
  2376.      * enabled for this component. Mouse motion events are enabled 
  2377.      * when one of the following occurs:
  2378.      * <p><ul>
  2379.      * <li>A <code>MouseMotionListener</code> object is registered 
  2380.      * via <code>addMouseMotionListener</code>.
  2381.      * <li>Mouse motion events are enabled via <code>enableEvents</code>.
  2382.      * </ul>
  2383.      * @param       e the mouse motion event.
  2384.      * @see         java.awt.event.MouseMotionEvent
  2385.      * @see         java.awt.event.MouseMotionListener
  2386.      * @see         java.awt.Component#addMouseMotionListener
  2387.      * @see         java.awt.Component#enableEvents
  2388.      * @since       JDK1.1
  2389.      */   
  2390.     protected void processMouseMotionEvent(MouseEvent e) {
  2391.         if (mouseMotionListener != null) {
  2392.             int id = e.getID();
  2393.             switch(id) {
  2394.               case MouseEvent.MOUSE_MOVED:
  2395.                 mouseMotionListener.mouseMoved(e);
  2396.                 break;
  2397.               case MouseEvent.MOUSE_DRAGGED:
  2398.                 mouseMotionListener.mouseDragged(e);
  2399.                 break;
  2400.             }
  2401.         }            
  2402.     }
  2403.  
  2404.     boolean postsOldMouseEvents() {
  2405.         return false;
  2406.     }
  2407.  
  2408.     /**
  2409.      * @deprecated As of JDK version 1.1
  2410.      * replaced by processEvent(AWTEvent).
  2411.      */
  2412.     public boolean handleEvent(Event evt) {
  2413.     switch (evt.id) {
  2414.       case Event.MOUSE_ENTER:
  2415.         return mouseEnter(evt, evt.x, evt.y);
  2416.  
  2417.       case Event.MOUSE_EXIT:
  2418.         return mouseExit(evt, evt.x, evt.y);
  2419.  
  2420.       case Event.MOUSE_MOVE:
  2421.         return mouseMove(evt, evt.x, evt.y);
  2422.  
  2423.       case Event.MOUSE_DOWN:
  2424.         return mouseDown(evt, evt.x, evt.y);
  2425.  
  2426.       case Event.MOUSE_DRAG:
  2427.         return mouseDrag(evt, evt.x, evt.y);
  2428.  
  2429.       case Event.MOUSE_UP:
  2430.         return mouseUp(evt, evt.x, evt.y);
  2431.  
  2432.       case Event.KEY_PRESS:
  2433.       case Event.KEY_ACTION:
  2434.         return keyDown(evt, evt.key);
  2435.  
  2436.       case Event.KEY_RELEASE:
  2437.       case Event.KEY_ACTION_RELEASE:
  2438.         return keyUp(evt, evt.key);
  2439.         
  2440.       case Event.ACTION_EVENT:
  2441.         return action(evt, evt.arg);
  2442.       case Event.GOT_FOCUS:
  2443.         return gotFocus(evt, evt.arg);
  2444.       case Event.LOST_FOCUS:
  2445.         return lostFocus(evt, evt.arg);
  2446.     }
  2447.         return false;
  2448.     }
  2449.  
  2450.     /**
  2451.      * @deprecated As of JDK version 1.1,
  2452.      * replaced by processMouseEvent(MouseEvent).
  2453.      */
  2454.     public boolean mouseDown(Event evt, int x, int y) {
  2455.     return false;
  2456.     }
  2457.  
  2458.     /**
  2459.      * @deprecated As of JDK version 1.1,
  2460.      * replaced by processMouseMotionEvent(MouseEvent).
  2461.      */
  2462.     public boolean mouseDrag(Event evt, int x, int y) {
  2463.     return false;
  2464.     }
  2465.  
  2466.     /**
  2467.      * @deprecated As of JDK version 1.1,
  2468.      * replaced by processMouseEvent(MouseEvent).
  2469.      */
  2470.     public boolean mouseUp(Event evt, int x, int y) {
  2471.     return false;
  2472.     }
  2473.  
  2474.     /**
  2475.      * @deprecated As of JDK version 1.1,
  2476.      * replaced by processMouseMotionEvent(MouseEvent).
  2477.      */
  2478.     public boolean mouseMove(Event evt, int x, int y) {
  2479.     return false;
  2480.     }
  2481.  
  2482.     /**
  2483.      * @deprecated As of JDK version 1.1,
  2484.      * replaced by processMouseEvent(MouseEvent).
  2485.      */
  2486.     public boolean mouseEnter(Event evt, int x, int y) {
  2487.     return false;
  2488.     }
  2489.  
  2490.     /**
  2491.      * @deprecated As of JDK version 1.1,
  2492.      * replaced by processMouseEvent(MouseEvent).
  2493.      */
  2494.     public boolean mouseExit(Event evt, int x, int y) {
  2495.     return false;
  2496.     }
  2497.  
  2498.     /**
  2499.      * @deprecated As of JDK version 1.1,
  2500.      * replaced by processKeyEvent(KeyEvent).
  2501.      */
  2502.     public boolean keyDown(Event evt, int key) {
  2503.     return false;
  2504.     }
  2505.  
  2506.     /**
  2507.      * @deprecated As of JDK version 1.1,
  2508.      * replaced by processKeyEvent(KeyEvent).
  2509.      */
  2510.     public boolean keyUp(Event evt, int key) {
  2511.     return false;
  2512.     }
  2513.  
  2514.     /**
  2515.      * @deprecated As of JDK version 1.1,
  2516.      * should register this component as ActionListener on component
  2517.      * which fires action events.
  2518.      */
  2519.     public boolean action(Event evt, Object what) {
  2520.     return false;
  2521.     }
  2522.  
  2523.     /**
  2524.      * Notifies this component that it has been added to a container
  2525.      * and if a peer is required, it should be created.
  2526.      * This method should be called by <code>Container.add</code>, and 
  2527.      * not by user code directly.
  2528.      * @see #removeNotify
  2529.      * @since JDK1.0
  2530.      */
  2531.     public void addNotify() {
  2532.         synchronized (getTreeLock()) {
  2533.             if (peer == null || peer instanceof java.awt.peer.LightweightPeer){
  2534.             if (peer == null) {
  2535.             peer = getToolkit().createComponent(this);
  2536.         }
  2537.  
  2538.                 // This is a lightweight component which means it won't be
  2539.                 // able to get window-related events by itself.  If any
  2540.                 // have been enabled, then the nearest native container must
  2541.                 // be enabled.
  2542.                 long mask = 0;
  2543.                 if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
  2544.                 mask |= AWTEvent.MOUSE_EVENT_MASK;
  2545.                 }
  2546.                 if ((mouseMotionListener != null) ||
  2547.                 ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0)) {
  2548.                 mask |= AWTEvent.MOUSE_MOTION_EVENT_MASK;
  2549.                 }
  2550.                 if (focusListener != null || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
  2551.                 mask |= AWTEvent.FOCUS_EVENT_MASK;
  2552.                 }
  2553.                 if (keyListener != null || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0) {
  2554.                 mask |= AWTEvent.KEY_EVENT_MASK;
  2555.                 }
  2556.                 if (mask != 0) {
  2557.                 parent.proxyEnableEvents(mask);
  2558.                 }
  2559.             } else {
  2560.                 // It's native.  If the parent is lightweight it
  2561.                 // will need some help.
  2562.                 if (parent != null && parent.peer instanceof java.awt.peer.LightweightPeer) {
  2563.                 new NativeInLightFixer();
  2564. //__SYMC__
  2565.                     // If setBounds is called before addNotify it will not correctly offset
  2566.                     // components within their parents. Therefore we have to do the 
  2567.                     // setBounds again here.
  2568.                     int nativeX = x;
  2569.                     int nativeY = y;
  2570.     
  2571.                     for(Component c = parent; (c != null) &&
  2572.                         (c.peer instanceof java.awt.peer.LightweightPeer); 
  2573.                         c = c.parent) {
  2574.     
  2575.                         nativeX += c.x;
  2576.                         nativeY += c.y;
  2577.                     }
  2578.                     peer.setBounds(nativeX, nativeY, width, height);
  2579. //__SYMC__
  2580.                 }
  2581.             }
  2582.             invalidate();
  2583.  
  2584.             int npopups = (popups != null? popups.size() : 0);
  2585.             for (int i = 0 ; i < npopups ; i++) {
  2586.                 PopupMenu popup = (PopupMenu)popups.elementAt(i);
  2587.                 popup.addNotify();
  2588.             }
  2589.             for (Component p = getParent(); p != null; p = p.getParent()) {
  2590.                 if (p instanceof Window) {
  2591.                     if (((Window)p).getWarningString() == null) {
  2592.                         //!CQ set newEventsOnly if appropriate/possible?
  2593.                     }
  2594.                     break;
  2595.                 }
  2596.             }
  2597.         }
  2598.     }
  2599.  
  2600.     /** 
  2601.      * Notifies this component that it has been removed from its
  2602.      * container and if a peers exists, it destroys it.
  2603.      * This method should be called by <code>Container.remove</code>, 
  2604.      * and not by user code directly.
  2605.      * @see #addNotify
  2606.      */
  2607.     public void removeNotify() {
  2608.         synchronized (getTreeLock()) {
  2609.           if (areInputMethodsEnabled()){
  2610.            InputContext inputContext = getInputContext();
  2611.            if (inputContext != null) {
  2612.                ComponentEvent e = new ComponentEvent(this, 
  2613.                                    ComponentEvent.COMPONENT_HIDDEN);
  2614.              inputContext.dispatchEvent(e);
  2615.            }   
  2616.          }
  2617.           int npopups = (popups != null? popups.size() : 0);
  2618.         for (int i = 0 ; i < npopups ; i++) {
  2619.             PopupMenu popup = (PopupMenu)popups.elementAt(i);
  2620.             popup.removeNotify();
  2621.         }
  2622.         if (peer != null) {
  2623.                 ComponentPeer p = peer;
  2624.                 p.hide();    // Hide peer first to stop system events such as cursor moves.
  2625.                 peer = null; // Stop peer updates.
  2626.                 Toolkit.getEventQueue().removeSourceEvents(this);
  2627.                 p.dispose();
  2628.         }
  2629.         }
  2630.     }
  2631.  
  2632.     /** 
  2633.      * @deprecated As of JDK version 1.1,
  2634.      * replaced by processFocusEvent(FocusEvent).
  2635.      */
  2636.     public boolean gotFocus(Event evt, Object what) {
  2637.     return false;
  2638.     }
  2639.  
  2640.     /** 
  2641.      * @deprecated As of JDK version 1.1,
  2642.      * replaced by processFocusEvent(FocusEvent).
  2643.      */
  2644.     public boolean lostFocus(Event evt, Object what) {
  2645.     return false;
  2646.     }
  2647.  
  2648.     /**
  2649.      * Returns the value of a flag that indicates whether 
  2650.      * this component can be traversed using
  2651.      * Tab or Shift-Tab keyboard focus traversal.  If this method
  2652.      * returns "false", this component may still request the keyboard
  2653.      * focus using <code>requestFocus()</code>, but it will not automatically
  2654.      * be assigned focus during tab traversal.
  2655.      * @return    <code>true</code> if this component is
  2656.      *            focus-traverable; <code>false</code> otherwise.
  2657.      * @since     JDK1.1
  2658.      */
  2659.     public boolean isFocusTraversable() {
  2660.         ComponentPeer peer = this.peer;
  2661.     if (peer != null) {
  2662.         return peer.isFocusTraversable();
  2663.     }
  2664.     return false;
  2665.     }
  2666.  
  2667.     /** 
  2668.      * Requests that this component get the input focus. 
  2669.      * <p>
  2670.      * This component's <code>gotFocus</code> method is called when this 
  2671.      * method is successful.  The component must be visible
  2672.      * on the screen for this request to be granted 
  2673.      * @see FocusEvent
  2674.      * @see #addFocusListener
  2675.      * @see #processFocusEvent
  2676.      * @see #isFocusTraversable
  2677.      * @since JDK1.0
  2678.      */
  2679.     public void requestFocus() {
  2680.         ComponentPeer peer = this.peer;
  2681.     if (peer != null) {
  2682.         if (peer instanceof java.awt.peer.LightweightPeer) {
  2683.         parent.proxyRequestFocus(this);
  2684.         } else {
  2685.         peer.requestFocus();
  2686.                 Toolkit.getEventQueue().changeKeyEventFocus(this);
  2687.         }
  2688.     }
  2689.     }
  2690.  
  2691.     /**
  2692.      * Transfers the focus to the next component.
  2693.      * @see       java.awt.Component#requestFocus
  2694.      * @see       java.awt.Component#gotFocus
  2695.      * @since     JDK1.1s
  2696.      */
  2697.      public void transferFocus() {
  2698.         nextFocus();
  2699.      }
  2700.  
  2701.     /**
  2702.      * @deprecated As of JDK version 1.1,
  2703.      * replaced by transferFocus().
  2704.      */
  2705.      public void nextFocus() {
  2706.         Container parent = this.parent;
  2707.     if (parent != null) {
  2708.         parent.transferFocus(this);
  2709.     }
  2710.      }
  2711.  
  2712.     /**
  2713.      * Adds the specified popup menu to the component.
  2714.      * @param     popup the popup menu to be added to the component.
  2715.      * @see       java.awt.Component#remove(java.awt.MenuComponent)
  2716.      * @since     JDK1.1
  2717.      */
  2718.     public synchronized void add(PopupMenu popup) {
  2719.     if (popup.parent != null) {
  2720.         popup.parent.remove(popup);
  2721.     }
  2722.         if (popups == null) {
  2723.             popups = new Vector();
  2724.         }
  2725.     popups.addElement(popup);
  2726.     popup.parent = this;
  2727.  
  2728.     if (peer != null) {
  2729.         if (popup.peer == null) {
  2730.         popup.addNotify();
  2731.         }
  2732.     }
  2733.     }
  2734.  
  2735.     /**
  2736.      * Removes the specified popup menu from the component.
  2737.      * @param     popup the popup menu to be removed.
  2738.      * @see       java.awt.Component#add(java.awt.PopupMenu)
  2739.      * @since     JDK1.1
  2740.      */
  2741.     public synchronized void remove(MenuComponent popup) {
  2742.         if (popups != null) {
  2743.         int index = popups.indexOf(popup);
  2744.         if (index >= 0) {
  2745.             PopupMenu pmenu = (PopupMenu)popup;
  2746.         if (pmenu.peer != null) {
  2747.             pmenu.removeNotify();
  2748.         }
  2749.         pmenu.parent = null;
  2750.         popups.removeElementAt(index);
  2751.         if (popups.size() == 0) {
  2752.             popups = null;
  2753.         }
  2754.         }
  2755.     }
  2756.     }
  2757.  
  2758.     /**
  2759.      * Returns the parameter string representing the state of this 
  2760.      * component. This string is useful for debugging. 
  2761.      * @return    the parameter string of this component.
  2762.      * @since     JDK1.0
  2763.      */
  2764.     protected String paramString() {
  2765.         String thisName = getName();
  2766.     String str = (thisName != null? thisName : "") + "," + x + "," + y + "," + width + "x" + height;
  2767.     if (!valid) {
  2768.         str += ",invalid";
  2769.     }
  2770.     if (!visible) {
  2771.         str += ",hidden";
  2772.     }
  2773.     if (!enabled) {
  2774.         str += ",disabled";
  2775.     }
  2776.     return str;
  2777.     }
  2778.  
  2779.     /**
  2780.      * Returns a string representation of this component and its values.
  2781.      * @return    a string representation of this component.
  2782.      * @since     JDK1.0
  2783.      */
  2784.     public String toString() {
  2785.     return getClass().getName() + "[" + paramString() + "]";
  2786.     }
  2787.  
  2788.     /**
  2789.      * Prints a listing of this component to the standard system output 
  2790.      * stream <code>System.out</code>. 
  2791.      * @see       java.lang.System#out
  2792.      * @since     JDK1.0
  2793.      */
  2794.     public void list() {
  2795.     list(System.out, 0);
  2796.     }
  2797.  
  2798.     /**
  2799.      * Prints a listing of this component to the specified output 
  2800.      * stream. 
  2801.      * @param    out   a print stream.
  2802.      * @since    JDK1.0
  2803.      */
  2804.     public void list(PrintStream out) {
  2805.     list(out, 0);
  2806.     }
  2807.  
  2808.     /**
  2809.      * Prints out a list, starting at the specified indention, to the 
  2810.      * specified print stream.
  2811.      * @param     out      a print stream.
  2812.      * @param     indent   number of spaces to indent.
  2813.      * @see       java.io.PrintStream#println(java.lang.Object)
  2814.      * @since     JDK1.0  
  2815.      */
  2816.     public void list(PrintStream out, int indent) {
  2817.     for (int i = 0 ; i < indent ; i++) {
  2818.         out.print("  ");
  2819.     }
  2820.     out.println(this);
  2821.     }
  2822.  
  2823.     /**
  2824.      * Prints a listing to the specified print writer.
  2825.      * @param  out  The print writer to print to.
  2826.      * @since JDK1.1
  2827.      */
  2828.     public void list(PrintWriter out) {
  2829.     list(out, 0);
  2830.     }
  2831.  
  2832.     /**
  2833.      * Prints out a list, starting at the specified indention, to 
  2834.      * the specified print writer.
  2835.      * @param out The print writer to print to.
  2836.      * @param indent The number of spaces to indent.
  2837.      * @see       java.io.PrintStream#println(java.lang.Object)
  2838.      * @since JDK1.1
  2839.      */
  2840.     public void list(PrintWriter out, int indent) {
  2841.     for (int i = 0 ; i < indent ; i++) {
  2842.         out.print("  ");
  2843.     }
  2844.     out.println(this);
  2845.     }
  2846.  
  2847.     /*
  2848.      * Fetch the native container somewhere higher up in the component
  2849.      * tree that contains this component.  
  2850.      */
  2851.     Container getNativeContainer() {
  2852.     Container p = parent;
  2853.     while (p != null && p.peer instanceof java.awt.peer.LightweightPeer) {
  2854.         p = p.getParent();
  2855.     }
  2856.     return p;
  2857.     }
  2858.  
  2859.     /* Serialization support.  
  2860.      */
  2861.  
  2862.     private int componentSerializedDataVersion = 1;
  2863.  
  2864.     private void writeObject(ObjectOutputStream s) 
  2865.       throws IOException 
  2866.     {
  2867.       s.defaultWriteObject();
  2868.       
  2869.       AWTEventMulticaster.save(s, componentListenerK, componentListener);
  2870.       AWTEventMulticaster.save(s, focusListenerK, focusListener);
  2871.       AWTEventMulticaster.save(s, keyListenerK, keyListener);
  2872.       AWTEventMulticaster.save(s, mouseListenerK, mouseListener);
  2873.       AWTEventMulticaster.save(s, mouseMotionListenerK, mouseMotionListener);
  2874.  
  2875.       s.writeObject(null);
  2876.     }
  2877.  
  2878.     private void readObject(ObjectInputStream s)
  2879.       throws ClassNotFoundException, IOException 
  2880.     {
  2881.       s.defaultReadObject();
  2882.       
  2883.       Object keyOrNull;
  2884.       while(null != (keyOrNull = s.readObject())) {
  2885.     String key = ((String)keyOrNull).intern();
  2886.  
  2887.     if (componentListenerK == key) 
  2888.       addComponentListener((ComponentListener)(s.readObject()));
  2889.  
  2890.     else if (focusListenerK == key) 
  2891.       addFocusListener((FocusListener)(s.readObject()));
  2892.  
  2893.     else if (keyListenerK == key) 
  2894.       addKeyListener((KeyListener)(s.readObject()));
  2895.  
  2896.     else if (mouseListenerK == key) 
  2897.       addMouseListener((MouseListener)(s.readObject()));
  2898.  
  2899.     else if (mouseMotionListenerK == key)
  2900.       addMouseMotionListener((MouseMotionListener)(s.readObject()));
  2901.  
  2902.     else // skip value for unrecognized key
  2903.       s.readObject();
  2904.  
  2905.       }
  2906.  
  2907.       if (popups != null) {
  2908.       int npopups = popups.size();
  2909.       for (int i = 0 ; i < npopups ; i++) {
  2910.           PopupMenu popup = (PopupMenu)popups.elementAt(i);
  2911.           popup.parent = this;
  2912.       }
  2913.       }
  2914.     }
  2915.  
  2916.     /**
  2917.      * This odd class is to help out a native component that has been
  2918.      * embedded in a lightweight component.  Moving lightweight
  2919.      * components around and changing their visibility is not seen
  2920.      * by the native window system.  This is a feature for lightweights,
  2921.      * but a problem for native components that depend upon the 
  2922.      * lightweights.  An instance of this class listens to the lightweight
  2923.      * parents of an associated native component (the outer class).
  2924.      *
  2925.      * @author  Timothy Prinzing
  2926.      */
  2927.     private final class NativeInLightFixer implements ComponentListener, ContainerListener {
  2928.  
  2929.     NativeInLightFixer() {
  2930.         lightParents = new Vector();
  2931.         Container p = parent;
  2932.         // stash a reference to the components that are being observed so that
  2933.         // we can reliably remove ourself as a listener later.
  2934.         for (; p.peer instanceof java.awt.peer.LightweightPeer; p = p.parent) {
  2935.  
  2936.         // register listeners and stash a reference 
  2937.         p.addComponentListener(this);
  2938.         p.addContainerListener(this);
  2939.         lightParents.addElement(p);
  2940.         }
  2941.         // register with the native host (native parent of associated native)
  2942.         // to get notified if the top-level lightweight is removed.
  2943.         nativeHost = p;
  2944.         p.addContainerListener(this);
  2945.  
  2946.         // kick start the fixup.  Since the event isn't looked at
  2947.         // we can simulate movement notification.
  2948.         componentMoved(null);
  2949.     }
  2950.  
  2951.     // --- ComponentListener -------------------------------------------
  2952.  
  2953.     /**
  2954.      * Invoked when one of the lightweight parents has been resized.
  2955.      * This doesn't change the position of the native child so it
  2956.      * is ignored.
  2957.      */
  2958.         public void componentResized(ComponentEvent e) {
  2959.     }
  2960.  
  2961.     /**
  2962.      * Invoked when one of the lightweight parents has been moved.  
  2963.      * The native peer must be told of the new position which is
  2964.      * relative to the native container that is hosting the
  2965.      * lightweight components.
  2966.      */    
  2967.         public void componentMoved(ComponentEvent e) {
  2968.         synchronized (getTreeLock()) {
  2969.         int nativeX = x;
  2970.         int nativeY = y;
  2971.         for(Component c = parent; (c != null) &&
  2972.             (c.peer instanceof java.awt.peer.LightweightPeer); 
  2973.             c = c.parent) {
  2974.             
  2975.             nativeX += c.x;
  2976.             nativeY += c.y;
  2977.         }
  2978.         if (peer != null) {
  2979.             peer.setBounds(nativeX, nativeY, width, height);
  2980.         }
  2981.         }
  2982.     }
  2983.  
  2984.     /**
  2985.      * Invoked when a lightweight parent component has been 
  2986.      * shown.  The associated native component must also be
  2987.      * shown if it hasn't had an overriding hide done on it.
  2988.      */
  2989.         public void componentShown(ComponentEvent e) {
  2990.         if (isShowing()) {
  2991.         synchronized (getTreeLock()) {
  2992.             if (peer != null) {
  2993.             peer.show();
  2994.             }
  2995.         }
  2996.         }
  2997.     }
  2998.  
  2999.     /**
  3000.      * Invoked when component has been hidden.
  3001.      */
  3002.         public void componentHidden(ComponentEvent e) {
  3003.         if (visible) {
  3004.         synchronized (getTreeLock()) {
  3005.             if (peer != null) {
  3006.             peer.hide();
  3007.             }
  3008.         }
  3009.         }
  3010.     }
  3011.  
  3012.     // --- ContainerListener ------------------------------------
  3013.  
  3014.     /**
  3015.      * Invoked when a component has been added to a lightweight
  3016.      * parent.  This doesn't effect the native component.
  3017.      */
  3018.         public void componentAdded(ContainerEvent e) {
  3019.     }
  3020.  
  3021.     /**
  3022.      * Invoked when a lightweight parent has been removed.
  3023.      * This means the services of this listener are no longer
  3024.      * required and it should remove all references (ie
  3025.      * registered listeners).
  3026.      */    
  3027.         public void componentRemoved(ContainerEvent e) {
  3028.         Component c = e.getChild();
  3029.         if (c == Component.this) {
  3030.         removeReferences();
  3031.         } else {
  3032.         int n = lightParents.size();
  3033.         for (int i = 0; i < n; i++) {
  3034.             Container p = (Container) lightParents.elementAt(i);
  3035.             if (p == c) {
  3036.             removeReferences();
  3037.             break;
  3038.             }
  3039.         }
  3040.         }
  3041.     }
  3042.  
  3043.     /**
  3044.      * Remove references to this object so it can be
  3045.      * garbage collected.
  3046.      */
  3047.     void removeReferences() {
  3048.         int n = lightParents.size();
  3049.         for (int i = 0; i < n; i++) {
  3050.         Container c = (Container) lightParents.elementAt(i);
  3051.         c.removeComponentListener(this);
  3052.         c.removeContainerListener(this);
  3053.         }
  3054.         nativeHost.removeContainerListener(this);
  3055.     }
  3056.  
  3057.     Vector lightParents;
  3058.     Container nativeHost;
  3059.     }
  3060. }
  3061.